Reputation: 357
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.
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.
Upvotes: 0
Views: 835
Reputation: 1072
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;
Upvotes: 1