Mohammad_Hosseini
Mohammad_Hosseini

Reputation: 2599

Do not change mysql column value if request variable is null in laravel eloquent

I need to check if $request->link_description is null then don't change anything or better to say don't touch the database column value.

is it possible to do so without doing an extra select query?

for example :

 /** 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]);

I mean this part :

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

instead of null I want to put something like :

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

But as I said before I'm looking for a way to not to run extra select query like above.

Upvotes: 0

Views: 170

Answers (1)

Thomas Van der Veen
Thomas Van der Veen

Reputation: 3226

It would be easier to build your data array before hand. Example:

// 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);

Upvotes: 2

Related Questions