从被点击的用户中选择所有图像

Selecting all images from user which is clicked

我最近开始学习Laravel 5.4版,但我有一些错误,到目前为止我找不到答案。我正在尝试制作用户友好的 url 并通过用户名而不是 ID 加载用户图像。

到目前为止,我已经在我的控制器中完成了这个

public function author(User $author) {

    $authorName = $author->username;

    $image = $author->with('author')->latestFirst()->paginate($this->limit);                          

    return view("author", compact('image', 'authorName'));

}

这是我的路线

Route::get('/author/{author}', [

    'uses' => 'HomeController@author',
    'as' => 'author'
]);

这是我的 link

<a href="{{ route('author', $categoryImage->author->username ) }} ">{{ $categoryImage->author->username }}</a>

这是我的图片模型

public function author()
{
    return $this->belongsTo(User::class, 'image_author');
}

和用户模型

public function images()
{
    return $this->hasMany(Image::class);
}

无论什么link我点击我总是得到这个错误

No query results for model [App\User].

我什至不确定我的控制器和路线是否正确。

这应该可以解决。 https://laravel.com/docs/5.4/routing#implicit-binding

将其放入您的用户模型中:

public function getRouteKeyName()
{
    return 'username';
}