Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Laracast13's avatar

laravel update query if else

Hello It is possible make check inside update query I want make update 'priceA' if $checkPrice is more 100

    Post::where('id', 1)
     ->update([
     'title' => $title;
  'text' => $text;
    if( $checkPrice > 100) { 'priceA'=> $priceA } 
    ]);
0 likes
9 replies
sr57's avatar

@www888

No problem, rewrite your check to another where condition

sr57's avatar
Post::where('id', 1)
->where('priceA','>=' ,$priceA)
-> ...
1 like
sr57's avatar

This test is a simple php test put it outside your update

if( $checkPrice > 100) {
 Post:: ...
}
Laracast13's avatar

@sr57

I need update title text and priceA if condition is true

Post::where('id', 1) ->update([ 'title' => $title; 'text' => $text; if( $checkPrice > 100) { 'priceA'=> $priceA } ]);

sr57's avatar

@www888

need update title text and priceA if condition is true

Yes, understood, you have all the tips

Just write 2 updates , one for little & text, and second for priceA if ....

MichalOravec's avatar
Level 75
Post::where('id', 1)->update([
     'title' => $title,
     'text' => $text
] + ($checkPrice > 100 ? [
    'priceA'=> $priceA
] : []));
4 likes

Please or to participate in this conversation.