Laravel雄辩的更新记录而无需从数据库加载 [英] Laravel eloquent update record without loading from database

查看:75
本文介绍了Laravel雄辩的更新记录而无需从数据库加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对laravel还是很陌生,我正在尝试从表单的输入中更新一条记录.但是我看到要更新记录,首先需要从数据库中获取记录. 不可能更新记录(已设置主键):

I'm quite new to laravel and I'm trying to update a record from form's input. However I see that to update the record, first you need to fetch the record from database. Isn't is possible to something like to update a record (primary key is set):

$post = new Post();
$post->id = 3; //already exists in database.
$post->title = "Updated title";
$post->save();

推荐答案

您可以简单地使用Query Builder而不是Eloquent,此代码直接更新数据库中的数据:)这是一个示例:

You can simply use Query Builder rather than Eloquent, this code directly update your data in the database :) This is a sample:

DB::table('post')
            ->where('id', 3)
            ->update(['title' => "Updated Title"]);

您可以在此处查看文档以获取更多信息: http://laravel.com/docs/5.0/queries#updates

You can check the documentation here for more information: http://laravel.com/docs/5.0/queries#updates

这篇关于Laravel雄辩的更新记录而无需从数据库加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆