hi all
I'm making a blog and i have a question : for the view post page, i use this route :
Route::group(array('domain' => 'blog.'.$domain), function(){
Route::get('/', 'BlogController@home');
Route::get('/{id}-{slug}', 'BlogController@viewPost');
});
i've read that laravel can automatically retrieve info from the database using route models so, i've done that :
Route::model('id','Post');
it's perfect, i get my data in the controller but, my post table uses data from other tables such as related medias and author information and these information are not retrieved. so is there a vay to make a request like this one
$post = Post::with('media','user')->find($id);
using route::model ?
You could use Route::bind()
.
Route::bind('id', function($id) {
return Post::with('media', 'user')->findOrFail($id);
});
Or you could pre-populate the $with property of the model. In this case every time you fetch a Post it will eager-load those too.
class Post extends Eloquent
{
protected $with = ['media', 'user'];
}
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community