草庐IT

updateOrCreate

全部标签

php - Laravel 5.6 - 在 updateOrCreate 后获取更改的值

我已经使用了laravel5.6并使用了updateOrCreate模型来添加或更新一些数据。但是我需要得到所有改变的值$q=Userssub::updateOrCreate(['userid'=>$uid],['model'=>$model]);结果如图所示如何获取更改数组?我试着用$u->changes和$u->changes->toarray()但两者都返回null。我该怎么做才能获得更改后的值? 最佳答案 Eloquent模型有两个protected数组,$original和$changes,分别包含从存储中获取时的属性和已

php - updateOrCreate 在 Laravel Eloquent 中不起作用

我想更新或创建模型。它根本不起作用。这是我的代码:foreach($ProfileRequestas$fieldname=>$value){ProfileRequest::updateOrCreate(['sm_id'=>$uid,'field_name'=>$fieldname],['sm_id'=>$uid,'field_name'=>$fieldname,'value'=>$value]);}这适用于插入:foreach($ProfileRequestas$fieldname=>$value){$req=newProfileRequest;$req->chef_id=$uid;$

php - updateOrCreate - Laravel 中的批量分配异常

我正在处理系统的CRUD部分。用户可以更新关键字。我想做的类似于INSERTIGNOREMySQL语句。我在一个foreach循环中并尝试更新关键字,但是我得到了一个MassAssignmentException错误我目前的代码如下,我做错了什么吗?//SaveTheKeywords...$kwords=Input::get('keywords');foreach($kwordsas$key=>$value){//SaveTheKeywords...Keywords::updateOrCreate(array('pack_id'=>$pack->pack_id,'keyword_tit

php - 使用 updateOrCreate 方法更新一对多关系

我的主要模型Tag与Product是一对多的关系,其中一个Tag可以有多个Products通过数据库上的tag_id关系分配给它们。在我的编辑View中,我允许用户编辑标签products。可以根据表单请求添加/编辑/删除这些产品。表单上的每个product字段都是从request()数组中选取的。例如:request('title'),request('price')。例如,我已将$title[$key]设置为request('title')数组。接下来,我的想法是根据请求数据循环遍历此标记和updateOrCreate的每个product。这里的问题是,无法检测该特定product

php - Laravel 5.4 - 'updateOrCreate' 方法。检查记录是否为 'updated' 或 'created'

使用updateOrCreate方法,有没有办法知道记录是更新还是创建?更新:我需要使用此功能为创建的记录返回201,或为更新的记录返回204。谢谢。 最佳答案 由于updateOrCreate返回模型实例。https://github.com/laravel/framework/blob/5.4/src/Illuminate/Database/Eloquent/Builder.php#L374您可以使用以下方法检查最近是否创建了记录:$instance->wasRecentlyCreatedhttps://github.com/l

php - Eloquent /Laravel : How to get last insert/update ID/instance of updateOrCreate()?

标题大部分是不言自明的。Eloquent有一个方法叫做updateOrCreate()记录在此处:https://laravel.com/docs/5.5/eloquent#other-creation-methods在某些情况下,这非常有用。然而,在执行updateOrCreate()之后,我需要更新/创建的对象或其主键或其ID。当然我可以做MyModel::where(...)->first()并再次提供所有这些数据,但这很笨拙并且可能是一些昂贵的请求。但是updateOrCreate()只返回true或false。有什么想法吗? 最佳答案

php - 在 Laravel 中使用 updateOrCreate 进行多数据插入

我在LaravelEloquentORM中使用了以下insertarray插入多个条目$i=0;foreach($exerciseValArrayas$kkey=>$exerciseValue){if($exerciseValue['exercise']==''||$exerciseValue['section_type']==''){returnResponse::JSON(array('errors'=>array('Errorinworkoutexercisesection')));}$exerciseData=explode(',',$exerciseValue['exerci

mysql - Laravel updateOrCreate 自增数据库

我的目的是如果该值存在则更新,否则在提交表单后在数据库表中插入一个新行。问题是,这里的函数在数据库表中添加新列而不是更新它们。这是我的功能:MyModel::updateOrCreate(array('myField'=>'myValue',))->where('myAutoIncrementalField','=','5')->where('myPrimaryKey','=','8');我的数据库表是这样的:1.myPrimaryKey(非自动增量,可在模型上填写。)2.myAutoIncrementalField(自动增量,不能在模型上填写。)提前谢谢你。

php - laravel updateOrCreate 方法

我的方法中有以下代码,我通过ajax将其发送到Controller方法:$newUser=\App\UserInfo::updateOrCreate(['user_id'=>Auth::user()->id,'about'=>$request->get('about'),'sec_email'=>$request->get('sec_email'),'gender'=>$request->get("gender"),'country'=>$request->get('country'),'dob'=>$request->get('dob'),'address'=>$request->g