创建和更新 Laravel Eloquent [英] Creating and Update Laravel Eloquent
本文介绍了创建和更新 Laravel Eloquent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
插入新记录或更新(如果存在)的简写是什么?
What's the shorthand for inserting a new record or updating if it exists?
<?php
$shopOwner = ShopMeta::where('shopId', '=', $theID)
->where('metadataKey', '=', 2001)->first();
if ($shopOwner == null) {
// Insert new record into database
} else {
// Update the existing record
}
推荐答案
这是lu cip"所谈论内容的完整示例:
Here's a full example of what "lu cip" was talking about:
$user = User::firstOrNew(array('name' => Input::get('name')));
$user->foo = Input::get('foo');
$user->save();
以下是最新版本的 Laravel 文档的更新链接
Below is the updated link of the docs which is on the latest version of Laravel
此处的文档:更新链接
这篇关于创建和更新 Laravel Eloquent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文