Shiva
289
7
Laravel

Finding out if a table is empty

Posted 3 months ago by Shiva

I'm trying to find out if a table is empty, I thought that by doing this

 if(!empty($address))
 {
        echo "not empty";
            
 }else{
        echo "empty";
          
 }

it would hit "empty" since the database is empty at the moment, but that isn't happening it keeps going to "not empty".

Here is the full code

public function addPostAddress(Request $request)
    {

        $user_id = Auth::user()->id;

        $address = new Address();

        $address->user_id = Auth::user()->id;
        $address->fill($this->getSafeAddressInput($request));

        if(!empty($address))
        {
            echo "not empty";
            
        }else{
            echo "empty";
          
        }

        return redirect()->back();
    }

Please sign in or create an account to participate in this conversation.