Hi ,sorry for english , i have a table sAdmin related with user table by id_users , in User model i have :
public function admin()
{
return $this->hasOne(Admin::class,'id_users','id');//'nome id tabella principale', 'id_primario_tabella_relzionata'
}
and in admin :
public function user()
{
return $this->belongsTo(User::class,'id','id_users');//'nome id tabella principale', 'id_primario_tabella_relzionata'
}
in my edit function i do
public function edit($id)
{
$data['record']=User::find($id);
//dd($data['record']);
echo view('admin/users/edit',$data);
}
But in view if i try to get property of admin's table f.e.:
$record->admin->notifiche i have error
Attempt to read property "notifiche" on null
To get the users related to an admin in Laravel, you can use the hasMany relationship method on the Admin model to define a one-to-many relationship with the User model.
In the Admin model:
public function users()
{
return $this->hasMany(User::class, 'id_users');
}
then you can do in your view:
@if ($record->admin?->notifiche) // check if notifiche is not null
{{ $record->admin->notifiche }}
@endif
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community