I'm trying to show a list of contacts for the logged in user. But obviously I'm doing something wrong.
I get a error on the contacts list page:
Trying to get property 'name' of non-object
User.php
public function contacts()
{
return $this->belongsToMany(Contact::class);
}
Contact.php
public function users()
{
return $this->belongsToMany(User::class);
}
ContactsController.php
public function index()
{
//
$user = Auth::user();
$user_contacts = $user->contacts()
return view('contacts.list')->with('contacts', $user_contacts);
}
list.blade.php
@foreach ($contacts as $contact)
* {{ $contact->name }} <br>
@endforeach
Table schema:
contacts:
- id
- created_at
- updated_at
- name
- address
users:
- id
- name
- password
- remember_token
- created_at
- updated_at
contact_user:
- contact_id
- user_id
User.phpdoesn't have anycontacts()relation function - Nitish Kumar$user_contacts = $user->contacts()must be$user_contacts = $user->contacts. - ako