php - How to get parent with only the first child in using eloquent orm? -


i'm trying active(i.e active = 1) albums album table has @ least 1 active image first active image in descending order of date created. i.e each album's image must last active image created.

my album model is:

class album extends model {     public function images()     {         return $this->hasmany('images','album_id');     }      public function scopeactive($query){         return $query->where('is_active', 1);     } } 

my images model is:

class images extends model {      public function scopeactive($query){         return $query->where('is_active', 1);     }      public function album(){         return $this->belongsto('album', 'album_id', 'id');     }  } 

so far i've come following:

$albums = album::with(['images' => function($query){             $query->orderby('created_at', 'desc');             $query->first();         }])         ->wherehas('images', function ($query) {             $query->active();         })         ->active()         ->get(); 

the above result give me following array image 1 album:

array ( [0] => array     (         [id] => 2         [name] => test gallery         [slug] => test-gallery         [description] => test gallery         [cover_image] =>          [is_active] => 1         [created_by] => 2         [updated_by] => 2         [deleted_at] =>          [images] => array             (             )      )  [1] => array     (         [id] => 3         [name] => gallery 2         [slug] => my-gallery-2         [description] => hello, second gallery.         [cover_image] =>          [is_active] => 1         [created_by] => 2         [updated_by] =>          [deleted_at] =>          [images] => array             (                 [0] => array                     (                         [id] => 9                         [album_id] => 3                         [image] => gallery_xfwjm0juzz.jpg                         [description] =>                          [is_active] => 1                         [created_by] => 2                         [updated_by] =>                          [deleted_at] =>                      )              )      )  ) 

can me achieve i'm trying do.

you should try like:

class album extends model {     public function image()     {         return $this->images()             ->active()             ->orderby('created_at', 'desc')             ->take(1)             ->first();     }      public function images()     {         return $this->hasmany('images','album_id');     }      public function scopeactive($query){         return $query->where('is_active', 1);     } }  $albums = album::active()->wherehas('images', function ($query) {     $query->active(); })->get();  foreach ($albums $album) {     var_dump($album->image()); } 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -