Laravel 8: Check if Record in Database is Empty


Im trying to test when a record is empty or not. Not sure why this isn’t working. user_id is my FK. When there is no record I like it to show that it’s empty and when it’s added to show it’s added. I’m manually adding removing a record to test it.


Schema::create('business_dashboards', function (Blueprint $table) {


namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class BusinessDashboard extends Model
    use HasFactory;

    protected $fillable = [

     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
    public function userprofile()
        return $this->belongsTo(User::class);


$businessDashboardUserId = BusinessDashboard::where('user_id', null)->first();

    if ($businessDashboardUserId) {
       dd('Is Null');
    } else {
       dd('Not Null');

DB Table
enter image description here


Try with whereNull() and count() for conditional

$businessDashboardUserId = BusinessDashboard::whereNull('user_id')->first();

    if (!$businessDashboardUserId->count()) {
       dd('Is Null');
    } else {
       dd('Not Null');

Or even nicer

if (!BusinessDashboard::whereNull('user_id')->exists()) {
   dd('Is Null');
} else {
   dd('Not Null');

Answered By – Luciano

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published