如何更新布尔类型列 - Laravel 5.5

问题描述 投票:0回答:4

我的数据库中有一个布尔列用于图像缩略图。一个选定的行将是缩略图(并且将具有值1),其他行将是0。如何将选定的行值更新为1和其他0.enter image description here

当我运行此更新功能时,它将每次更新行(对于ID)(当然)。

public function update(Request $request, $id)
{
    $id = (int) $id;

    $image = HotelGallery::where('id', $id)
                            ->update(['is_thumbnail' => '1']);

    return response()->json(['success' => 'Done']);
}

enter image description here

ID 23有价值1,我怎么更新这个is_thumbnail行反对ID是3,将1和其他is_thumbnail行应该0在一个查询?

php laravel-5.5
4个回答
0
投票

我知道你想在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']);
}

0
投票

您可以在PDOMysqli中使用多个查询。

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();
}

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作为值,将上述代码中的'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中做到这一点的最好方法,但这应该适用于现在。


0
投票

最好的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]);
            }
        });
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.