kwestionable
kwestionable

Reputation: 496

Checking if column is null Laravel

I have this column ('time_out') in a table of my database:

enter image description here

As you can see the time_out is null, and when I do this query:

$checkertimeout = Attendance::where('emp_id', $request->emp_id)
                  ->where('attend_date', $date)->get(['time_out']);

I get this output:

[{"time_out":null}]

But when I use it in an if-else statement:

if ($checkertimeout->isEmpty())
{
    echo "works";
}
else
{
    echo "wont work";
}

It display won't work.

Any help is appreciated, I'm new to Eloquent query :)

Upvotes: 5

Views: 23712

Answers (5)

Ripon Uddin
Ripon Uddin

Reputation: 714

Hit it

$checkertimeout = Attendance::where('emp_id', $request->emp_id)
                ->where('attend_date', $date)->first();

At Blade File If Else

{{ $checkertimeout->time_out !=null ? 'Wont Work' : 'work' }} // when this condition $checkertimeout->time_out !=null true its run after "?" when its false it's running after ":" term

For Controller You can also dd or echo like below :

echo $checkertimeout->time_out !=null ? 'Wont Work' : 'work'; // you can replace echo with return too

Upvotes: 3

Japs
Japs

Reputation: 1052

First your query is getting all the records.

you should do like this in your query:

$checkerTimeOut = Attendance
            ::where([['emp_id', $request->emp_id], ['attend_date', $date]])
            ->first();

// This is to get a single record

then if you use if else statement you should also call the column you want to stablish.

if($checkerTimeOut->time_out->isEmpty()
{
  dd('not empty');
}
else
{
  dd('this record is empty');
}

you can also do like this:

if($checkerTimeOut->time_out)
{
  dd('not empty');
}
else
{
  dd('this record is empty');
}

or should be like this:

$checkerTimeOut->time_out
    ? 'not empty'
    : 'this record is empty';

Upvotes: 1

Kenny Horna
Kenny Horna

Reputation: 14241

First of all, when you do this:

$checkertimeout = Attendance
          ::where('emp_id', $request->emp_id)
          ->where('attend_date', $date)
          ->get(['time_out']); // <---

You are getting a Collection of all the time_out fields of every Attendance models that match your criteria (the wheres). This means, something like this:

=> Illuminate\Database\Eloquent\Collection {#3323
     all: [
       App\Attendance {#3332
         attend_date: "2019-10-06 22:01:29",
       },
       App\Attendance {#3340
         attend_date: "2019-10-06 22:01:29",
       },
       App\Attendance {#314
         attend_date: null,
       },
     ],
   }

You can get more info in the Eloquent section of the documentation.

Now, if your query is designed to get only one record, you could use the ->first() method instead of the get() one. This method will return the first Attendance object that matches your criteria.

$checkertimeout = Attendance
          ::where('emp_id', $request->emp_id)
          ->where('attend_date', $date)
          ->first(); // <---

PS: If you only need to retrieve the time_out value, you could also add the ->select('attend_date') to the query:

$checkertimeout = Attendance
          ->select('time_out'); // <---
          ::where('emp_id', $request->emp_id)
          ->where('attend_date', $date)
          ->first(); // <---

With these clarifications, let's go to your question. If you want to check if a field is null.. you could just make use of the PHP function is_null().

Using the collection

$checkertimeout = Attendance
          ::where('emp_id', $request->emp_id)
          ->where('attend_date', $date)
          ->get(['time_out']);

if(is_null($checkertimeout->first()->time_out))
{
    // ...
}

Using a single object

$checkertimeout = Attendance
          ->select('time_out'); // <---
          ::where('emp_id', $request->emp_id)
          ->where('attend_date', $date)
          ->first(); // <---

if(is_null($checkertimeout->time_out)) // <----
{
    // ...
}

Side note

As a side note, if you want to avoid the null results for that value, you could add another contraint in your query:

$checkertimeout = Attendance
          ::where('emp_id', $request->emp_id)
          ->where('attend_date', $date)
          ->whereNotNull('time_out') // <-----
          ->get(['time_out']);

Upvotes: 8

Moshiur
Moshiur

Reputation: 685

Because $checkertimeout got a collection as

[
    {"time_out":null}
]

so its no longer empty and it returns

"wont work"

Upvotes: 0

Tharaka Dilshan
Tharaka Dilshan

Reputation: 4499

Event of the time_out is null, $checkertimeout variable is not empty. Because $checkertimeout is a collection. and it has one item in it.

As you can see like this.

[
    {"time_out":null}
]

Since I don't know what you are trying to do, If you want the Attendance only where time_out is not null, You can do this.

 $checkertimeout = Attendance::where('emp_id', $request->emp_id)
                    ->where('attend_date', $date)
                    ->whereNotNull('time_out')
                    ->get(['time_out']);

Upvotes: 0

Related Questions