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

Shawdow's avatar

How can I Assigning Null value for pots_id value using if else statement


$user = new Post;

if ($user->type === "blue"){

              if (DB::table('posts')
                        ->where([
                            ['type', '=', 'blue'],
                            ['login_status', '=', 'no'],
                                   
                        ]))
              {   
                 // How can assign null value for post_id  

                 $user->post_id = ???; 
                    
              }

     
        else
        {
                $id = DB::table('posts')
                        ->where([
                            ['login_status', '=', 'yes'],
                             ['type', '=', 'leaf'],
                        ])
                        ->first()
                        ->id;
                $user->post_id = $id;
        
        }
    
    }

      $user->save();

0 likes
11 replies
tisuchi's avatar

Just update your column-

$user->post_id = null; 

Note: if you need to assign in DB, just update the column and make sure your column is nullable in DB.

3 likes
Shawdow's avatar

@tisuchi

It is updating in phpmyadmin but the error??

The Response content must be a string or object implementing __toString(), "boolean" given.

1 like
MaverickChan's avatar

the logic should not be that complicated .

in your database design , add default value null to post_id.

when something meet true , then let the post_id = $id.

it can save you a lot of lines of code.

and , in if else statement , make it simple , or you will be crazy

1 like
tisuchi's avatar

Than its not the issue of Null, may be something else.

3 likes
Shawdow's avatar

@tisuchi

I have written but no use..

$user->post_id =
                      DB::table('posts')->select('id')
                       ->update(['posts.post_id' => Null]);
1 like
tisuchi's avatar

Literally there are some issues here-

$user = new Post;

if ($user->type === "blue"){
  1. You cannot access property by new instance.
  if (DB::table('posts')
            ->where([
                ['type', '=', 'blue'],
                ['login_status', '=', 'no'],
                       
            ]))
  { 
  1. You need to use either get() or first() to access any particular object.
3 likes
Shawdow's avatar

@tisuchi

I have changed but same error

if(DB::table('posts')
                        ->where([
                            ['type', '=', 'blue'],
                            ['login_status', '=', 'no'],
                        ])->first()->id)
1 like
Shawdow's avatar

@tisuchi this type of error i have never saw in laravel but not able to identify... anyway thanks for feedback

1 like
tisuchi's avatar
tisuchi
Best Answer
Level 70

@Naveena

I hope here is the solution that I can think of it for you...

Way 1:

$user = new Post;
//everything you need to add here as a new entry
// $user->post_id = null;
// $user->save()

Way 2:

$user = Post::find(pass a post id);

//now check whether post type == blue or not
if ($user->type == 'blue' AND $user->login_status == 'no'){
  $user->post_id = null;
  $user->save();
}
else {
  $user->post_id = $id;
  $user->save();
}
3 likes

Please or to participate in this conversation.