Automatic download?
Instead of doing something like this (which I do dozens of times throughout the site):
$posts = Post::with('user')
->with('image')
->get();
Can I automatically call with('image')
on call with('user')
? So in the end I could only do:
$posts = Post::with('user')
->get();
And still an impatient loading image
?
+3
user4454001
source
to share
2 answers
Here's another solution that works like a charm!
class Post extends Model {
protected $table = 'posts';
protected $fillable = [ ... ];
protected $hidden = array('created_at','updated_at');
public function user()
{
return $this->belongsTo('App\Models\User');
}
public function userImage()
{
return $this->belongsTo('App\Models\User')->with('image');
}
}
$posts = Post::with('userImage')->get();
Using this, you can still use your user messages $posts = Post::with('user')->get();
when you don't want to make an extra call to fetch images.
+1
source to share