Support the ongoing development of Laravel.io →
Requests Database Eloquent

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 ?

Last updated 3 years ago.
0

nobody knows ?

Last updated 3 years ago.
0

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'];
}
Last updated 3 years ago.
0

perfect thanks a lot

Last updated 3 years ago.
0

thanks @tlgreg this helped me too.

Last updated 8 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.