如何更新布尔类型列 - Laravel 5.5
how to update Boolean type column - Laravel 5.5
我的数据库中有一个用于图像缩略图的布尔列。一个选定的行将是缩略图(并且将具有值 1
),而其他行将是 0
。如何将选定的行值更新为 1
和其他 0
.
当我运行这个更新函数时,它每次都会根据ID更新行。
public function update(Request $request, $id)
{
$id = (int) $id;
$image = HotelGallery::where('id', $id)
->update(['is_thumbnail' => '1']);
return response()->json(['success' => 'Done']);
}
ID 2
和 3
具有值 1
,我如何根据 [= 更新此 is_thumbnail 行32=]ID 即 3
,将是 1
而其他 is_thumbnail 行应该是 0
一个查询?
//To Update row two as shown in above image
query="UPDATE $data_base_name.$data_base_table SET `is_thumbnail`=0 WHERE `id`=2";
result=mysqli_query($conn,(query));
//To Update row two as shown in above image (being more specific)
query="UPDATE $data_base_name.$data_base_table SET `is_thumbnail`=false WHERE `id`=2";
result=mysqli_query($conn,(query));
上面的代码会将第二行 is_thumbail
的值更改为 0,因为 1 将上面代码中的 'false' 或 '0' 更改为 '1' 或 'true'。
//Update to improved question (updating all rows)
query="
UPDATE $data_base_name.$data_base_table SET `is_thumbnail`=true WHERE `id`=2;
UPDATE $data_base_name.$data_base_table SET `is_thumbnail`=false WHERE `id`!=2;";
result=mysqli_multi_query($conn,(query));
while (mysqli_next_result($conn)){;}//this is to free result set for multi query
上述查询会将第二行设置为 'true' 或“1”,每隔一行设置为 'false' 或“0”。
如果您是 php 的新手,请阅读 SQL 注射!!!另见 https://secure.php.net/manual/en/mysqli.multi-query.php
//Update to improved question (eloquent format)
Table::where('id', '=',2 )->update(['is_thumbnail' => 'true']);
Table::where('id', '!=',2 )->update(['is_thumbnail' => 'false']);
这不是在 laravel 中执行此操作的最佳方法,但目前应该可行。
PDO 示例:
$db = new PDO("mysql:host=localhost;dbname=test", 'root', '');
// works regardless of statements emulation
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, 0);
$thumbnail_id = 2;
$sql = 'UPDATE tab SET tab.is_thumbnail = false;' . "\n" .
'UPDATE tab SET tab.is_thumbnail = true WHERE tab.id = ?';
try {
$stmt = $db->prepare($sql);
$stmt->execute([$thumbnail_id]);
}
catch (PDOException $e)
{
echo $e->getMessage();
die();
}
我明白你想要 hotel_id 只有 is_thumbnail=1:
public function update(Request $request, $id)
{
$id = (int) $id;
$hotelGallery = HotelGallery::where('id', $id)
->pluck('hotel_id')->toArray();
$hotel_id = array_get($hotelGallery, '0', 0);
// Set is_thumbnail = 0 for all image of hotel_id
HotelGallery::where('hotel_id', $hotel_id)
->update(['is_thumbnail' => 0]);
$image = HotelGallery::where('id', $id)->update(['is_thumbnail' => 1]);
return response()->json(['success' => 'Done']);
}
最好的Laravel方法是将此功能集成到模型中:
class HotelGallery extends Model
{
protected static function boot ()
{
parent::boot();
self::saving(function($model) {
if($model->attributes['is_thumbnail'] == 1)
{
(new self)->where('hotel_id', $model->attributes['hotel_id'])->where('id', '<>', $model->attributes['id'] ?? 0)->update(['is_thumbnail' => 0]);
}
});
}
}
我的数据库中有一个用于图像缩略图的布尔列。一个选定的行将是缩略图(并且将具有值 1
),而其他行将是 0
。如何将选定的行值更新为 1
和其他 0
.
当我运行这个更新函数时,它每次都会根据ID更新行。
public function update(Request $request, $id)
{
$id = (int) $id;
$image = HotelGallery::where('id', $id)
->update(['is_thumbnail' => '1']);
return response()->json(['success' => 'Done']);
}
ID 2
和 3
具有值 1
,我如何根据 [= 更新此 is_thumbnail 行32=]ID 即 3
,将是 1
而其他 is_thumbnail 行应该是 0
一个查询?
//To Update row two as shown in above image
query="UPDATE $data_base_name.$data_base_table SET `is_thumbnail`=0 WHERE `id`=2";
result=mysqli_query($conn,(query));
//To Update row two as shown in above image (being more specific)
query="UPDATE $data_base_name.$data_base_table SET `is_thumbnail`=false WHERE `id`=2";
result=mysqli_query($conn,(query));
上面的代码会将第二行 is_thumbail
的值更改为 0,因为 1 将上面代码中的 'false' 或 '0' 更改为 '1' 或 'true'。
//Update to improved question (updating all rows)
query="
UPDATE $data_base_name.$data_base_table SET `is_thumbnail`=true WHERE `id`=2;
UPDATE $data_base_name.$data_base_table SET `is_thumbnail`=false WHERE `id`!=2;";
result=mysqli_multi_query($conn,(query));
while (mysqli_next_result($conn)){;}//this is to free result set for multi query
上述查询会将第二行设置为 'true' 或“1”,每隔一行设置为 'false' 或“0”。
如果您是 php 的新手,请阅读 SQL 注射!!!另见 https://secure.php.net/manual/en/mysqli.multi-query.php
//Update to improved question (eloquent format)
Table::where('id', '=',2 )->update(['is_thumbnail' => 'true']);
Table::where('id', '!=',2 )->update(['is_thumbnail' => 'false']);
这不是在 laravel 中执行此操作的最佳方法,但目前应该可行。
PDO 示例:
$db = new PDO("mysql:host=localhost;dbname=test", 'root', '');
// works regardless of statements emulation
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, 0);
$thumbnail_id = 2;
$sql = 'UPDATE tab SET tab.is_thumbnail = false;' . "\n" .
'UPDATE tab SET tab.is_thumbnail = true WHERE tab.id = ?';
try {
$stmt = $db->prepare($sql);
$stmt->execute([$thumbnail_id]);
}
catch (PDOException $e)
{
echo $e->getMessage();
die();
}
我明白你想要 hotel_id 只有 is_thumbnail=1:
public function update(Request $request, $id)
{
$id = (int) $id;
$hotelGallery = HotelGallery::where('id', $id)
->pluck('hotel_id')->toArray();
$hotel_id = array_get($hotelGallery, '0', 0);
// Set is_thumbnail = 0 for all image of hotel_id
HotelGallery::where('hotel_id', $hotel_id)
->update(['is_thumbnail' => 0]);
$image = HotelGallery::where('id', $id)->update(['is_thumbnail' => 1]);
return response()->json(['success' => 'Done']);
}
最好的Laravel方法是将此功能集成到模型中:
class HotelGallery extends Model
{
protected static function boot ()
{
parent::boot();
self::saving(function($model) {
if($model->attributes['is_thumbnail'] == 1)
{
(new self)->where('hotel_id', $model->attributes['hotel_id'])->where('id', '<>', $model->attributes['id'] ?? 0)->update(['is_thumbnail' => 0]);
}
});
}
}