我不知道如何在某些条件下将字段设置为空。这应该是一个简单的查询,但我无法在可为空的字段上设置正确的 NULL 值。
Product::where('id', $product_list)->update(['family_id' => null]);
Run Code Online (Sandbox Code Playgroud)
也不
Product::where('id', $product_list)->update(['family_id' => DB::raw(null)]);
Run Code Online (Sandbox Code Playgroud)
成功了。两种情况都编译得很好,但生成的查询似乎是错误的,因为我收到 SQL 错误。在第一种情况下,错误是:
SQLSTATE[HY093]: Invalid parameter number (SQL: update `products` set `family_id` = ?, `products`.`updated_at` = 2019-11-12 08:41:07 where `id` = ?)
Run Code Online (Sandbox Code Playgroud)
第二个的错误是:
QLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ', `products`.`updated_at` = ? where `id` = ?' at line 1 (SQL: update `products` set `family_id` = , `products`.`updated_at` = 2019-11-12 08:21:50 where `id` = ?)
Run Code Online (Sandbox Code Playgroud)
在第一种情况下,似乎 NULL 没有被添加到准备好的查询的参数中,而在第二种情况下,NULL 值被作为查询中的空值插入。有什么简单的方法可以将值设置为NULL?
您应该首先考虑以下事项。
1)family_id表中的列可以为空。如果不是则设置可为空。
2)确保您拥有model 列$fillable的属性。Productfamily_id
确保该特定产品的$product_list退货。id如果$product_list是产品数据的集合/数组,则使用如下:
Product::where('id', $product_list->id)->update(['family_id' => null]);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5228 次 |
| 最近记录: |