【问题标题】:Laravel No query results for modelLaravel 没有模型的查询结果
【发布时间】:2015-06-10 15:08:11
【问题描述】:

您好,我已经使用 Laravel 5 3 天了,但遇到了问题。我正在测试我的编辑页面,我收到错误No query results for model。这是示例链接http://localhost:8000/profile/sorxrob/editsorxrob 有一个用户名。这是我的路线:

Route::bind('user', function($user) {
    return App\User::where('username', $user)->first();
});

Route::get('profile/{user}', 'ConsultaProfileController@viewProfile');

Route::get('profile/{user}/edit', 'ConsultaProfileController@edit');

在我的ConsultaProfileController 中,这里是公共函数:

public function edit($id)
{
    $account = User::findOrFail($id);
    return view('consulta.edit', compact('account'));
}

每当我尝试http://localhost:8000/profile/sorxrob/edit 时,它都会给我错误No query results for model,但是当我将路由更改为Route::get('profile/{id}/edit', 'ConsultaProfileController@edit'); 时,它可以工作,但现在URL 应该是http://localhost:8000/profile/6/edit,其中6 是sorxob 的ID。我想使用用户名,但我不知道问题出在哪里。

【问题讨论】:

    标签: laravel laravel-5


    【解决方案1】:

    这是因为你已经有了一个User 对象。

    您已将user 绑定到User 对象。所以您收到的参数是User 对象,而不是id。所以不用再查询了。

    随便用

    public function edit($user)
    {
        return view('consulta.edit', compact('user'));
    }
    

    【讨论】:

    • 没问题,如果解决了您的问题,请将其标记为已回答。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-16
    • 1970-01-01
    • 2019-12-04
    • 1970-01-01
    • 2014-12-26
    相关资源
    最近更新 更多