首页 > 解决方案 > 在 laravel 笔记工作中路由 /author/{username}

问题描述

嗨,我将此网址称为 http://127.0.0.1:8000/author/Tohidda,我看到 404 not found 错误。我使用这条路线但不工作!...我的路线和控制器是

  Route::get('/author/{username}','UserController@author')->name('dashboard.author');
public function author(User $user , Course $course,$username)
{
    $courses = Learning::with('course')->take(1)->latest()->get();
    $categories = Category::where('parent','0')->get();
    return view('Home.dashboard.author',compact('categories','user','courses','course','favorites'));
}

注意:Tohidda 我在用户表中的用户名

标签: laravellaravel-5eloquentlaravel-4laravel-7

解决方案


该路线可能已正确访问。Laravel 引发 a404 是因为它找不到用户模型。您正在使用用户名访问用户,而默认情况下您应该使用id用户的访问它。

您可以使用name,但您需要指示 laravel 这样做。您需要将 定义name为键。请参阅:https ://laravel.com/docs/7.x/eloquent#eloquent-model-conventions


推荐阅读