php - 方法 Illuminate\Database\Eloquent\Collection::update 不存在

标签 php laravel

有人帮我解决这个错误吗?当我尝试更新帖子时会发生这种情况, 这是我的更新函数

public function update(Requests\PostRequest $request, $id)
    {
        $post = Post::findOrFail($id);
        $data = $this->handleRequest($request);
        $post->update($data);
        return redirect('/blog/post')->with('message','Your posts was updated successfully');
    }

这是我的 handleRequest 函数

private function handleRequest($request)
    {
        $data = $request->all();

        if ($request->hasFile('image')) {
            $image = $request->file('image');
            $fileName = $image->getClientOriginalName();
            $destination = $this->uploadPath;

            $image->move($destination, $fileName);

            $data['image'] = $fileName;
        }

        return $data;
    }

最佳答案

您正在访问一个集合实例,您需要访问底层模型:

foreach($post as $p){//loop through an Eloquent collection instance
    $p->fill($data);//mass assign the new values
    $p->save();//save the instance
}

关于php - 方法 Illuminate\Database\Eloquent\Collection::update 不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54610311/

相关文章:

php - SQL_CALC_FOUND_ROWS 不起作用

php - 商店时间表的数据库架构

php - ubuntu 16.04 中 laravel 安装错误

Laravel homestead 多个站点选择哪个本地可访问

php - 使用 Laravel 的队列发送消息

php - 媒体 Assets 管理解决方案php、mysql、云

php - Laravel orderByRaw 正在从联合查询中删除结果

javascript - 在递归中使用全局变量是一种好习惯吗

php - Laravel - 我应该存储模型结果以避免多个数据库查询吗?

php - 拉维尔 : How to send variable from one page to another in blade