How to match two tables id laravel - laravel-5

#foreach($SearchResult as $fri => $friends)
{{ $confriend[$fri]->friend_one }}
{{ $confriend[$fri]->friend_two }}
#endforeach
$friends has searched friend list and $confriend[$fri]->friend_one and $confriend[$fri]->friend_two are the record which are already friends with logged-in user, so I want to match those ids with each other so I can show the user that they are your friends.

Related

How to included joined data in a loop

I am displaying a list on my blade view which contains data from the display table: name, school and IP.
The school is saved as a school id in this table.
The Schools table contains the list of ids and related schools.
I want to use the school names from the schools table and display them on my blade view.
I can get this data separately.
This gets all the data from the display table
$records = DB::table('display')->paginate(10);
This gets all the school name from the schools table.
$schools = DB::table('schools')
->join('display', 'schools.id', '=', 'display.school')
->select('schools.name')
->get();
To display the data from the display table I'm using a foreach loop.
#foreach ($records as $record)
{{ $record->name }}
{{ $record->school }}
{{ $record->ip }}
#endforeach
I don't know how to insert the school name into the space where currently $record->school appears. $record->school displays the school ID, but I want to display the school name which is saved in the school table.
Display model
public function school()
{
return $this->belongsTo('App\School');
}
School model
public function display()
{
return $this->hasMany('App\Display');
}
If you have models and Eloquent relations, you can simply use it:
Controller
$records = Display::with('school')->paginate(10);
Blade view
#foreach ($records as $record)
{{ $record->name }}
{{ $record->school->name }} <!-- school name via relation -->
{{ $record->ip }}
#endforeach
Display Model
class Display extends Model
{
public function school()
{
return $this->hasOne('App\School');
}
}
You really should be using Eloquent as it makes life so much easier. However, using DB, you need code similar to below, where you are joining schools as a relationship to display.
$records = DB::table('display')
->join('schools', 'schools.id', '=', 'display.school')
->paginate(10);
#foreach ($records as $record)
{{ $record->name }}
{{ $record->schools->name }}
{{ $record->ip }}
#endforeach

How to access other fields on a three way pivot in Laravel?

Here's what my architecture looks like:
The idea is that a user have a role on a given group. Of course, the user belongs to many groups, and a user can have multiple roles (in different groups)
I'm trying to get all the groups a user belongs to.
Please forgive me for not using the convention to name the pivot table as groups_roles_users, I decided this way for readability.
So, in the User model I say:
public function groups() {
return $this->belongsToMany('\App\Group', 'roles_in_groups');
}
In a view I do:
#foreach ($user->groups as $group)
{{ $group->name }}
#endforeach
works perfectly.
Now I want to access the roles table. I want to print something like: In {{ $group->name }} you're a {{ $group->role->name }}, obviously, It doesn't work like that.
If I use the pivot property, it will give me only the user_id and group_id, not even the role_id to search with an static method.
SO, how can I accomplish that?
I'm using Laravel 5.7
Maybe try:
public function roles() {
return $this->belongsToMany('\App\Role', 'roles_in_groups');
}
$user = \User::with(['roles','groups'])->first();
$role_name = $user->role->name
$group_name = $user->group->name
https://laravel.com/docs/5.7/eloquent-relationships#eager-loading

Property does not exist on this collection instance

I have a Client model that hasMany Appointment models. And an Appointment belongs to a Client. I am trying to return results that show me the client's name as well as their list of appointments on an index blade. Here is my code so far:
Client Model
public function appointment()
{
return $this->hasMany(Appointment::class);
}
Appointment Model
public function client()
{
return $this->belongsTo(Client::class);
}
Controlller
$clients = Client::with('appointment')->get();
//dd($clients);
return view('scheduler')->withclients($clients);
Blade
#foreach($clients as $client
{{ $client->name }}
{{ $client->appointment->id }}
#endforeach
How can I print $client name and their list of appointments? I have tried other help regarding this issue but I am not clear where I am going wrong.
So you relationship is a One-To-Many relationship. You have a lot of appointments then.
You can iterate over the clients list first and in the inner loop over the appointments:
#foreach($clients as $client)
{{ $client->name }}
#foreach($clients->appointment as $appointment)
{{ $appointment->id }}
{{ $appointment->name }}
#endforeach
#endforeach
Anyway, it might be easier to go over the appointment model, because it's in relation to exactly one client:
#foreach(Appointment::all() as $appointment)
{{ $appointment->name }}
{{ $appointment->client->name }}
#endforeach

Fetch data form pivot tables with one query using laravel eloquent

What is the best way to get the 3rd table data with one query.
User : id | name
Event : id | user_id
Likes : id | user_id | event_id
Here is my Eloquent Query
$events = Event::with('likes','users')->get();
return view('events',compact('events'));
In views i am iterating over all events like this,
User has multiple events as well as multiple likes in relation model.
#foreach($events as $event)
{{ $event->name }}
{{ $like_id }} //show like id if it is liked by the user
// I have to execute another loop only to check if it is liked or not
#foreach($row->likes as $like)
#if(Auth::id() == $like->user_id)
Liked
#endif
#endforeach
#endforeach
Now i need to check if the logged in user has liked this event or not ?
along with all the events whether they are liked or not ?
You can try as:
#foreach($events as $event)
{{ $event->name }}
{{ $event->likes->count() }}
#if($event->likes->contains('user_id', Auth::id()))
Liked
#endif
#endforeach

Laravel Many To Many Method Issue

I have a many to many relationship between the models User & Notification.
User Model:
public function notifications()
{
return $this->belongsToMany('Yeayurdev\Models\Notification', 'notifications_user', 'user_id', 'notification_id')
->withPivot('notifier');
}
Notification Model:
public function user()
{
return $this->belongsToMany('Yeayurdev\Models\User', 'notifications_user', 'notification_id', 'user_id');
}
Pivot Table:
ID USER_ID NOTIFICATION_ID
1 2 1
In my view, I retrieve a list of all notifications for a user like so.
#foreach (Auth::user()->notifications as $notification)
{{ $notification->user->username }}
#endforeach
The problem is that when I try to get the username of the user with that notifications, I get the error Undefined property: Illuminate\Database\Eloquent\Collection::$username. "Username" is the column on my users table which is just a string. I don't see where I'm going wrong because if I just do "$notificatin->user" it gives me the collection.
Thanks!
As you said, the relationship is many-to-many, so $notification->user in your case is returning a collection of User models, NOT a single user. If you need only a first user then just do
{{ $notification->user()->first()->username }}
If you need to print out all users for every notification, then you will need a loop here to go through all users.
#foreach (Auth::user()->notifications as $notification)
#foreach($notification->user as $notificationUser)
{{ $notificationUser->username }}
#endforeach
#endforeach
I would recommend renaming user() relationship to users() to avoid confusion in the future.

Resources