Philosopher
Philosopher

Reputation: 29

having issue while trying to use model route binding with laravel

By using this code I'm trying to return the post from posts table which has the same id by {post}.

Route::model('posts', 'Posts');
Route::get('post/{post}', function(Posts $post){
    return $post;
});

But It returns:

Argument 1 passed to {closure}() must be an instance of Posts, string given

What's the problem? (I'm a beginner)

Upvotes: 0

Views: 113

Answers (1)

lukasgeiter
lukasgeiter

Reputation: 152870

The name in Route::model needs to match the parameter in your route:

Route::model('post', 'Posts');
Route::get('post/{post}', function(Posts $post){
    return $post;
});

Upvotes: 1

Related Questions