dimanche 31 décembre 2017

Eager loader eloquent model inside of model inside of another model

I have the following querie:

  $content_movie=$this->content::with([


        'credits'=>function($query){

        $query->select('id','credit_type','department','job','characterr','person_id');
                                    },
        'collections.images'=>function($query){
            $query->select('collections_id','id','file_path');
        },

        //colecciones

        'collections'=>function($query){
        $query->select('id','name','overview');
        },

        'collections.images',

        'collections.content'=>function($query){
        $query->select('id','collections_id','name','slug');
        },

        'collections.content.images'=>function($query){
        $query->select('id','content_id','file_path','vote_count')->where('is_backdrop',0)->orderBy('vote_count','desc');
        },

        //contenido propio del elemento que estamos buscando

        'images'=>function($query){
        $query->select('content_id','is_backdrop','file_path')->orderBy('vote_count', 'desc')->first();
        },
        'genres',

        'credits.person.images',


        ])->where('slug',$args['slug'])->get(['id','name','collections_id','overview','release_date','status','video','vote_average']);


    $content=$content_movie->toJson();


To put you in situation, each 'collection' has movies and each movie has images

collections --> movies --> images.

For example:

we have the kill bill collection --> kill bill 1 --> image of kill bill 1 | kill bill 2 --> image of kill bill 2 | kill bill 3 --> image of kill bill 3

if in the above code I modify the collection.content.images.... in this way for only take 5 images of each film:

'collections.content.images'=>function($query){
        $query->select('id','content_id','file_path','vote_count')->where('is_backdrop',0)->orderBy('vote_count','desc')->limit(5);
        },

is going to take the 5 first images of kill bill 1 but not the first 5 of the 2ºpart and the 3º part.


if instead of put limit(5) I put the next code:

    'collections.content.images'=>function($query){
        $query->select('id','content_id','file_path','vote_count')->where('is_backdrop',0)->orderBy('vote_count','desc')->first();
        },

to take the first image of each film only takes the first image of kill bill 1.


how can I modify my code to get one image of each film, calling a model inside of another model, inside of another model?

I have read the documentation, but I not be able to figure out how to do this.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2DD5c54
via IFTTT

Aucun commentaire:

Enregistrer un commentaire