- Laravel 5.5
I have a model (request) that returns customer id, it's receiving it from a different machine so I want to treat non-existent values (i.e. id that doesn't exist returning the eloquent as null).
so for:
public function customer()
{
return $this->belongsTo('App\Customer', 'site_user');
}
-
I've tried the following:
public function getSiteUserAttribute() { if (!$this->relationLoaded('customer')) $this->load('customer');
return $this->getRelation('customer') ?: $this->nullCustomer(); }
and nullCustomer()
:
private function nullCustomer()
{
$nonExist = 'non-exist-customer';
$siteUser = new \Illuminate\Support\Collection;
$siteUser->first_name = $nonExist;
$siteUser->last_name = $nonExist;
$siteUser->email = $nonExist;
return $siteUser;
}
Yet Laravel is returning an error I can't really make sense of:
Undefined property: App\Request::$site_user (View: C:\xampp700\htdocs\stackOverflow\resources\views\request\index.blade.php)
it's obviously related to getSiteUserAttribute()
which extrapolates site_user
, but I can't understand what's the issue.
I can isset()
for every place that this relation is called, but i'm working with a smart framework, so I doubt that would be the best practice.
Just to reiterate, i'm trying ot treat null belongsTo()
that wouldn't break the view.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire