skip to Main Content
  $position = getPositionLocation($id);            
$extra = UserExtra::where('user_id', $posid)->first();            
if ($position = 1) {                
$extra->free_left += 1;            
} else {                
$extra->free_right += 2;            
}           
 $extra->save();

This is were my error is
$extra->free_left += 1;

            $extra->free_left +== 1;

2

Answers


  1. Check if property free_left exists on the $extra object.

    Login or Signup to reply.
  2. #1 – You can use is_null and the !, just to prevent that NULL error from $extra.

    $position = getPositionLocation($id);
    $extra = UserExtra::where('user_id', $id)->first();
    
    if(!is_null($extra)) {
        if ($position == 1) { //! don't do just one equal (=), it will change variable.
            $extra->free_left += 1;
        } else {
            $extra->free_right = 2;
        }
        $extra->save();
    }
    

    #2 – In laravel you can use the ->firstOrFail() to prevent this error without more condition on your code.

    $extra = UserExtra::where('user_id', $id)->firstOrFail();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search