在 Laravel 5.8 中,如果行存在,如何只更新几列?
In Laravel 5.8 how update only a few columns if row exists?
我正在使用 updateOrInsert 在数据库中插入一行或更新它以防它已经存在。
来自手册:
https://laravel.com/docs/5.8/queries
Update Or Insert
Sometimes you may want to update an existing record in the database or create it if no matching record exists. In this scenario, the updateOrInsert method may be used. The updateOrInsert method accepts two arguments: an array of conditions by which to find the record, and an array of column and value pairs containing the columns to be updated.
The updateOrInsert method will first attempt to locate a matching database record using the first argument's column and value pairs. If the record exists, it will be updated with the values in the second argument. If the record can not be found, a new record will be inserted with the merged attributes of both arguments:
在我的例子中,我不想覆盖(更新)所有列,只覆盖 updated_at 列。
在 MySql 中,我使用 INSERT 和 ON DUPLICATE KEY UPDATE 指定唯一要更新的列。
如何在 Laravel 中使用 updateOrInsert 执行此操作?感谢您的任何建议。
DB::table('products')->updateOrInsert(
['upc' => $request->get('upc'),],
['upc' => $request->get('upc'),
'name' => $request->get('name'),
'created_at' => ...,
'updated_at' => ...]
);
如果您查看查询生成器的代码,您会发现 Laravel 也在执行两个查询:
/**
* Insert or update a record matching the attributes, and fill it with values.
*
* @param array $attributes
* @param array $values
* @return bool
*/
public function updateOrInsert(array $attributes, array $values = [])
{
// See if record exists (query 1)
if (! $this->where($attributes)->exists()) {
// Do an insert (query 2)
return $this->insert(array_merge($attributes, $values));
}
// Do an update (query 2)
return (bool) $this->take(1)->update($values);
}
您可以复制此代码并更改:
return (bool) $this->take(1)->update($values);
到
return (bool) $this->take(1)->update(['updated_at' => '2019-08-31 12:34:45']);
如果您确实想使用 INSERT with ON DUPLICATE KEY UPDATE
,您应该使用 RAW 查询。 Laravel 查询生成器不支持仅 MySQL 方法。
原始查询应如下所示:
DB::statement('INSERT INTO `products` (a,b,c) VALUES (1,2,3) ON DUPLICATE KEY UPDATE `updated_at` = NOW();');
如果我正确理解您的问题,您应该在查询中指定重复的列并更新他们的 updated_at
列。例如
DB::table('products')->where('duplicate_key', 'duplicate_value')->updateOrInsert(
['updated_at' => Carbon::now()],
);
试试这个更新
products::where('id', $id)->update(['upc' => $request->input('upc'), 'upc' => $request->get('upc'),'name' => $request->get('name')]);
return redirect()->back();
我正在使用 updateOrInsert 在数据库中插入一行或更新它以防它已经存在。
来自手册: https://laravel.com/docs/5.8/queries
Update Or Insert Sometimes you may want to update an existing record in the database or create it if no matching record exists. In this scenario, the updateOrInsert method may be used. The updateOrInsert method accepts two arguments: an array of conditions by which to find the record, and an array of column and value pairs containing the columns to be updated.
The updateOrInsert method will first attempt to locate a matching database record using the first argument's column and value pairs. If the record exists, it will be updated with the values in the second argument. If the record can not be found, a new record will be inserted with the merged attributes of both arguments:
在我的例子中,我不想覆盖(更新)所有列,只覆盖 updated_at 列。
在 MySql 中,我使用 INSERT 和 ON DUPLICATE KEY UPDATE 指定唯一要更新的列。
如何在 Laravel 中使用 updateOrInsert 执行此操作?感谢您的任何建议。
DB::table('products')->updateOrInsert(
['upc' => $request->get('upc'),],
['upc' => $request->get('upc'),
'name' => $request->get('name'),
'created_at' => ...,
'updated_at' => ...]
);
如果您查看查询生成器的代码,您会发现 Laravel 也在执行两个查询:
/**
* Insert or update a record matching the attributes, and fill it with values.
*
* @param array $attributes
* @param array $values
* @return bool
*/
public function updateOrInsert(array $attributes, array $values = [])
{
// See if record exists (query 1)
if (! $this->where($attributes)->exists()) {
// Do an insert (query 2)
return $this->insert(array_merge($attributes, $values));
}
// Do an update (query 2)
return (bool) $this->take(1)->update($values);
}
您可以复制此代码并更改:
return (bool) $this->take(1)->update($values);
到
return (bool) $this->take(1)->update(['updated_at' => '2019-08-31 12:34:45']);
如果您确实想使用 INSERT with ON DUPLICATE KEY UPDATE
,您应该使用 RAW 查询。 Laravel 查询生成器不支持仅 MySQL 方法。
原始查询应如下所示:
DB::statement('INSERT INTO `products` (a,b,c) VALUES (1,2,3) ON DUPLICATE KEY UPDATE `updated_at` = NOW();');
如果我正确理解您的问题,您应该在查询中指定重复的列并更新他们的 updated_at
列。例如
DB::table('products')->where('duplicate_key', 'duplicate_value')->updateOrInsert(
['updated_at' => Carbon::now()],
);
试试这个更新
products::where('id', $id)->update(['upc' => $request->input('upc'), 'upc' => $request->get('upc'),'name' => $request->get('name')]);
return redirect()->back();