Reputation: 1103
I've started to learn Laravel version 5.4 recently but I have some error that I can't find a answer so far. I'm trying to make user-friendly urls and to load images of user by username not by ID.
So far I've made this in my controller
public function author(User $author) {
$authorName = $author->username;
$image = $author->with('author')->latestFirst()->paginate($this->limit);
return view("author", compact('image', 'authorName'));
}
This in my route
Route::get('/author/{author}', [
'uses' => 'HomeController@author',
'as' => 'author'
]);
And this is my link
<a href="{{ route('author', $categoryImage->author->username ) }} ">{{ $categoryImage->author->username }}</a>
This is my Image model
public function author()
{
return $this->belongsTo(User::class, 'image_author');
}
And User model
public function images()
{
return $this->hasMany(Image::class);
}
Whatever link I click I always get this error
No query results for model [App\User].
I'm not even sure if my controller and routes are correct.
Upvotes: 1
Views: 25
Reputation: 9942
This should solve it. https://laravel.com/docs/5.4/routing#implicit-binding
Put this in your user model:
public function getRouteKeyName()
{
return 'username';
}
Upvotes: 1