【问题标题】:Do not change mysql column value if request variable is null in laravel eloquent如果请求变量在 laravel eloquent 中为空,请不要更改 mysql 列值
【发布时间】:2018-09-16 09:25:45
【问题描述】:

我需要检查$request->link_description is null 是否不要更改任何内容,或者最好不要更改数据库列值。

是否可以在不进行额外选择查询的情况下这样做?

例如:

 /** update ad Seat info */
 $Update_Seat = AdSeat::where('id', $id)->where('owner_id', $owner_id)
 ->update(['seat_name' => $seat_name,
         'seat_url' => $seat_url,
         'logo_src' => ($logo_src) ? $logo_src : null,
         'category_id' => $category_id,
         'plan' => $plan,
         'price_sale' => $price_sale,
         'link_description' => ($link_description) ? $link_description : null,
         'description' => ($description) ? $description : null,
         'income_percentage' => $income_percentage,
         'status' => STATUS_TO_APPROVE]);

我的意思是这部分:

'link_description' => ($link_description) ? $link_description : null,

而不是null 我想写一些类似的东西:

'link_description' => ($link_description) ? $link_description : AdSeat::whereId(5)->value('link_description),

但正如我之前所说,我正在寻找一种不运行上述额外选择查询的方法。

【问题讨论】:

    标签: php mysql laravel orm eloquent


    【解决方案1】:

    事先构建数据数组会更容易。示例:

    // Do not add $link_description here
    $data = [
        'seat_url' => $seat_url,
        ...
    ];
    
    // But add it if $link_description is not null
    if (!is_null($link_description)) {
        $data['link_description'] = $link_description;
    }
    
    $Update_Seat = AdSeat::...->update($data);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-02
      • 2021-01-15
      • 1970-01-01
      • 1970-01-01
      • 2016-12-09
      • 2016-08-17
      • 2018-07-05
      相关资源
      最近更新 更多