Laravel:消息为 'Method update does not exist.' 的 BadMethodCallException

Laravel: BadMethodCallException with message 'Method update does not exist.'

当我尝试使用此代码更新模型时:

public function updateMixedtape($slug, Request $request)
{
    $mix = Mix::where('slug', $slug)->get();
    $mix->update($request->all());
    return redirect('dashboard/mixes');
}

我收到一条错误消息,指出方法 update 不存在。但是,如果我修改视图以发送 radio_show_id 而不是 slug 并尝试将代码更改为如下内容:

public function updateMixedtape(Request $request)
{
    $mix = Mix::findOrFail($request->radio_show_id);
    $mix->update($request->all());
    return redirect('dashboard/mixes');
}

代码执行无任何错误。

令我困惑的是,如果我在调用 update 方法的行之前执行类似 return $mix; 的操作,我会得到两种方法的相似数据。

正如 shock_gone_wild 在我的问题的评论部分所建议的那样 $mix = Mix::where('slug', $slug)->get(); 是 return 一个集合而不是一个模型。这是因为 Model::where() 方法可以 return 零条、一条或多条记录,具体取决于是否存在满足设置条件的记录。

按照建议,我使用 $mix = Mix::where('slug', $slug)->first(); 来获取满足条件的第一条记录。