I need to alias when I do a Laravel eager loading:
$posts = Post::with(array(\'images as main_image\' => function($query) // do not run
{
Perfect! you give me the idea. Finally I've done this:
Post.php
public function main_image()
{
return $this->hasMany('FoodImage')->where('number','=','0');
}
public function gallery_images()
{
// for code reuse
return $this->main_image();
}
PostController.php
$posts = Post::with:with('main_image')->with('gallery_images')
->where('id', '=', $id)
->get();