我有一个页面模型.它在数据库表中有以下列:

  • 身份证件
  • 标题
  • 形象
  • body

I want to update only "形象" column value.

这是我的代码:

public function delImage($path, $身份证件) {
    $page = Page::find($身份证件);
    $page->where('形象', $path)->update(array('形象' => 'asdasd'));
    \File::delete($path);
}

it throws me an error, that i am trying to use where() on a non-object. How can i correctly update my "形象" column value?

推荐答案

You may try this:

Page::where('id', $id)->update(array('image' => 'asdasd'));

There are other ways too but no need to use Page::find($id); in this case. But if you use find() then you may try it like this:

$page = Page::find($id);

// Make sure you've got the Page model
if($page) {
    $page->image = 'imagepath';
    $page->save();
}

你也可以使用:

$page = Page::findOrFail($id);

So, it'll throw an exception if the model with that id was not found.

Laravel相关问答推荐

Laravel Livewire与自定义JS有关的多姆问题

验证判断请求的值是否存在于另一个表 Laravel 9 中

如何使用动态翻译键翻译 Laravel硬编码字符串(还有 1 个错误)..(验证异常类)

调用字符串上的成员函数

Laravel:BelongstoMany 关系实现

Laravel:子文件夹中的 lang 文件

Laravel:如何强制使用 HTTPS?

在 laravel 如何将额外的数据传递给 mutators 和 accessors

在 laravel 中更改时区

将 Laravel Socialite 与 API 一起使用?

如何访问 Carbon 对象类型?

Laravel 和 PHPStorm 项目的 gitignore 中包含什么?

Laravel Blade - 如果满足条件,则添加一个类

Laravel 5 - 判断日期是否是今天

在 Laravel artisan 命令中使用详细

你如何在 Laravel 中的每个响应上强制一个 JSON 响应?

Carbon解析到 ISO8601

Laravel Nova 显示用户友好的资源名称

同一模型上的 Laravel 父/子关系

Laravel:Redis 无法建立连接:[tcp://127.0.0.1:6379]