Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何为Laravel 5.2雄辩模型编写“关于重复密钥更新”?_Php_Laravel - Fatal编程技术网

Php 如何为Laravel 5.2雄辩模型编写“关于重复密钥更新”?

Php 如何为Laravel 5.2雄辩模型编写“关于重复密钥更新”?,php,laravel,Php,Laravel,我有一个表,它的唯一键是app_id+channel_id,但它没有主键 我想使用on duplicated key update语句在Laravel eloquent模型中插入/更新last_update_time字段,我找到了update或create方法,但在我的项目中没有识别它,我应该如何编写我的模型?来自Laravel文档参见最后一部分 Laravel中有两种方法: 其他创建方法 可以使用其他两种方法通过批量指定属性来创建模型:firstOrCreate和firstOrNew。firs

我有一个表,它的唯一键是app_id+channel_id,但它没有主键


我想使用on duplicated key update语句在Laravel eloquent模型中插入/更新last_update_time字段,我找到了update或create方法,但在我的项目中没有识别它,我应该如何编写我的模型?

来自Laravel文档参见最后一部分 Laravel中有两种方法:

其他创建方法

可以使用其他两种方法通过批量指定属性来创建模型:firstOrCreate和firstOrNew。firstOrCreate方法将尝试使用给定的列/值对定位数据库记录。如果在数据库中找不到该模型,将插入具有给定属性的记录

firstOrNew方法(如firstOrCreate)将尝试在数据库中查找与给定属性匹配的记录。但是,如果找不到模型,将返回一个新的模型实例。请注意,firstOrNew返回的模型尚未持久化到数据库中。您需要手动调用save以保持它:

// Retrieve the flight by the attributes, or create it if it doesn't exist...

$flight = App\Flight::firstOrCreate(['name' => 'Flight 10']);

// Retrieve the flight by the attributes, or instantiate a new instance...

$flight = App\Flight::firstOrNew(['name' => 'Flight 10']);
您是否尝试过更新或创建数组$attributes,数组$values=[]

只要使用具有自动增量id列的表,就可以使用Model::updateOrCreate,其中第一个参数作为包含要查找的属性的关联数组,第二个参数作为包含任何新值的关联数组

请参阅.DB::statement的方法定义,插入到'table\u name``col\u name\u 1`、`col\u name\u 2`值?在重复键上更新col_name_1=col_name_1[val_1,val_2]的值

Laravel允许使用DB::statment$query、$bindings=[]的原始数据库语句


需要确保的一点是,第二个参数必须是索引数组,而不是关联数组。如果它是关联的,它将抛出一个常规SQL异常。

请检查这一点,但为什么我不能使用此方法?像firstOrCreate一样,我只是一个模型,扩展了'illumb\Database\elount\model`