Fetch data form pivot tables with one query using laravel eloquent - laravel-5

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

Related

Laravel : how to show CategoryTitle in show views from pivot table

I want show category_title in post views.i'm using pivot for category_id and post_id
Post Model:
public function categories()
{
return $this->belongsToMany(Category::class,'category_post','post_id','category_id');
}
Show.blade.php
{{$post->categories->category_title}}
but show me this error
Property [category_title] does not exist on this collection instance.
You can not access it directly, with belongsToMany you're getting multiple objects.
To access this you need to follow it as below.
#foreach($post->categories as $category)
{{ $category->category_title }}
#endforeach
Or you can access it as below.
{{ $post->categories->pluck('category_title ')->implode(',') }}

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

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.

How to match two tables id laravel

#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.

How to query data from a pivot table and display it using blade?

I am trying to query data from my pivot table and display it on my view
My View
#foreach ($catalog_downloads as $catalog_download)
<td>
<?php $frequencies = $catalog_download->export_frequencies()->get(); ?>
{{{ $frequencies }}}
</td>
#endforeach
When I do {{{ $frequencies }}} I got this :
[{"id":1,"name":"Semi Annual","created_at":"2015-01-14 15:44:59","updated_at":"2015-01-14 15:44:59","pivot":{"catalog_download_id":104,"export_frequency_id":1}},{"id":2,"name":"Quaterly","created_at":"2015-01-14 15:45:06","updated_at":"2015-01-14 15:45:06","pivot":{"catalog_download_id":104,"export_frequency_id":2}},{"id":3,"name":"Monthly","created_at":"2015-01-14 15:45:13","updated_at":"2015-01-14 15:45:13","pivot":{"catalog_download_id":104,"export_frequency_id":3}}]
But I don't want that. I want only the name.
So I did this {{{ $frequencies->name or '' }}} and now nothing come up at all. :(
More details
Here is how I define my relation.
public function export_frequencies(){
return $this->belongsToMany('ExportFrequency','export_frequency_catalog_download','catalog_download_id','export_frequency_id');
}
Can someone tell me what did I do wrong ?
By default, Laravel only selects the two foreign key from the pivot table. To change that add withPivot to your relationship definition:
public function export_frequencies(){
return $this->belongsToMany('Frequency')->withPivot('name');
});
Then you can access it through the pivot object:
#foreach($frequencies as $frequency)
{{ $frequency->pivot->name }}
#endforeach
Edit
I guess I've been confused by your question title. If you just want to display data from ExportFrequency you don't need withPivot and you can simply do this:
#foreach($frequencies as $frequency)
{{ $frequency->name }}
#endforeach

Resources