jeudi 15 octobre 2020

add data in same query in laravel

i am new in laravel, i tried code for array

 $videos = ResourcesVideo::where('resource_type', 'video')->take(2)->get();

        $music = ResourcesVideo::where('resource_type', 'music')->take(2)->get();

        $mediation = ResourcesVideo::where('resource_type', 'mediation')->take(2)->get();

        $tedtalk = TedTalk::take(2)->get();


        $response['data'] = [
                                "ted_talks" => $tedtalk,
                                "video" => $videos,
                                "music" => $music,
                                "mediation" => $mediation,
                            ];

i got right data like

{
  "data": {
    "ted_talks": [
    ],
    "videos": [
    ],
    "music": [
    ],
    .
    .
    .
  }
}

i have question for how can i reduce that repeated code. please check my code it shown same logic repeated 3 times.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3iYdqsY
via IFTTT

Aucun commentaire:

Enregistrer un commentaire