dimanche 18 avril 2021

Take random Elements multiple times from Backend using Laravel

I have never worked with Laravel so please have mercy with my poor soul.

What I want to do is get, lets say 20 randome files from my Backend, which has only 5 Elements (yes, there is a usecase for that).

Here is what i tried:

public function getIDs($id, $number) {
    $volume = Volume::findOrFail($id);
    $this->authorize('access', $volume);

    $array[] = array();
    for($i = 0; $i < $number; $i++){
        array_push($array, $volume->files()
        ->inRandomOrder()
        ->take(1)
        ->pluck('id'));
    }
    return $array;

    return $volume->files()
        ->inRandomOrder()
        ->take($number)
        ->pluck('id');
    }

The second retrun would at least work if i only request 5 images. What i want to do is to just go throught a for-loop and add elements to a array and return that array. This however doesn't work.

any tips?

tyvm




Aucun commentaire:

Enregistrer un commentaire