Ad

Laravel: How To Check If Data In Datatables Is Valid Or Not?

I am trying to get a data from another table through foreign key, i want to check if that data is valid or not, i mean is it empty or not.

for example if the foreing key data is empty, i want to return the null.

here is what i tried to get it right but not nothing works fine if the foreign key is empty.

enter image description here

and here is the relationship between the two tables

 public function driver()
{
    return $this->belongsTo(Driver::class, 'Driver_id');
}

And again i want to clear my point i want to check if the foreign key data is empty or not, if empty i want return null.

Ad

Answer

Just check if the Driver_id is set.

$driverName = null;
if(isset($bus->Driver_id) && $bus->driver && $bus->driver->first_name) $driverName = $bus->driver->first_name.' '.$bus->driver->last_name.' '.$bus->driver->third_name;
return $driverName;
Ad
source: stackoverflow.com
Ad