Laravel Eager Loading a Relation with Eloquent - php

I'm attempting to load all of the items in my Lists database, while applying optional filters if they're specified. With these, I'd like to load the count of subscribers for each of the lists. I can do this via the normal $list->subscribers()->count() call within the foreach loop in the view, but can I do this through the actual pagination function?
Inside of my ListsRepo.php file:
<?php namespace Acme\Repos;
use Lists;
class DbListsRepo implements ListsRepoInterface {
public function getPaginated(array $params)
{
$list = new Lists;
// See if there are any search results that need to be accounted for
if ($params['search'] != null) $list = $list->where('name', 'LIKE', "%".$params['search']."%");
// See if we need to restrict the results to a single user
if ($params['user'] != null) $list = $list->where('userID', $params['user']);
// Check if the data should be sorted
if ($this->isSortable($params)) $list = $list->orderBy($params['sortBy'], $params['direction']);
return $list->paginate(10);
}
public function isSortable(array $params)
{
return $params['sortBy'] and $params['direction'];
}
}
Inside of my index.blade.php file:
....
#if ($lists->count())
#foreach ($lists as $list)
<tr>
<td><h4>{{ $list->name }}</h4></td>
<td><p>{{ $list->subscribers()->count() }}</p></td>
</tr>
#endforeach
#endif
...
So is there a way to properly attach the subscribers count to my getPaginated function? The current implementation results in an N+1 scenario.

You should be able to do it by including the eager-load in your getPaginated function:
public function getPaginated(array $params) {
$list = Lists::newQuery();
// See if there are any search results that need to be accounted for
if ($params['search'] != null) $list->where('name', 'LIKE', "%".$params['search']."%");
// See if we need to restrict the results to a single user
if ($params['user'] != null) $list->where('userID', $params['user']);
// Check if the data should be sorted
if ($this->isSortable($params)) $list->orderBy($params['sortBy'], $params['direction']);
$list->with('subscribers');
return $list->paginate(10);
}
And then in your blade you can simply do count($list->subscribers) because the subscribers will be preloaded into your list models.
You have to use PHP's count() on the results array, not SQL's COUNT when it comes to eager loading, as eager loading is done with a single select statement on the related table.

Related

Laravel - Eager Loading

I'm trying to understand Eager Loading using Laravel to avoid generating a lot of unnecessary queries. I want to get 15 last added Posts and also get their rates from relationship of my rates table (before I was getting Posts and later in foreach I was calling for $item->avgRate() that creates 15 additional queries :S).
My Post model:
public function rates()
{
return $this->hasMany(Rate::class);
}
public function scopeLastAdded($query, $limit = 15)
{
return $query->latest()->limit($limit)->with('rates')->get();
}
This works, for each post, I'm also getting all rates, but the main goal is to make some function to calculate avg rate for each post and not retrieve all rates. I created a new method:
public function avgRate()
{
return number_format($this->rates()->avg('rate'), 1, '.', '');
}
When I use with('avgRate') my model fails:
Call to a member function addEagerConstraints() on string
How can I get avgRate in some clean way with my last 15 Posts to perform only 2 queries and not 16?
Expected output:
// Post view
#foreach ($posts as $post)
<div>{{ $post->title }}</div>
<div>{{ $post->avgRate }}</div> //I want to get data without performing 15 queries
#endforeach
I would use a subquery to achieve this. Also, to make things a little bit cleaner, you can create a scope for fetching the rating:
public function scopeWithRating($query)
{
$rating = Rate::selectRaw('AVG(rate)')
->whereColumn('post_id', 'posts.id')
->getQuery();
$query->select('posts.*')
->selectSub($rating, 'rating');
}
... and to use it, you'd do:
Post::withRating()->get();
Now, your Post objects will also contain a column rating, and that has been done with, essentially, a single query.
Here's an example to illustrate this.

Manipulating data within an object

Hi I am new to Laravel and php. In my database each student has three marks on each subject and I need to retrieve them and send to the view the average mark for each student.
I tried doing it like below, but the returned value is an object (e.g. [5,4,3]) and it doesn't let me count the average. Please advise how I can operate with data within the object.
$students = Student::all();
foreach ($students as $student) {
$mathPoints = Point:: where('subject_id', 1)
->where('student_id', $student->id)
->pluck('points');
}
I tried turning it into an array by (array) method, but I couldn't calculate the sum of values with array_sum after.
Update: my Point model:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Point extends Model
{
//Get the student the credit points are related to
public function student()
{
return $this->belongsTo('App/Models/Student');
}
//Get the subject the credit points are related to
public function subject()
{
return $this->belongsTo('App/Models/Subject');
}
}
use Model::avg('columnName') to calculate average .
read more here : https://laravel.com/docs/5.4/queries#aggregates
$students = Student::all();
foreach ($students as $student){
$mathPoints = Point::where(['subject_id'=>1,'student_id'=>$student->id])->avg('points');
$student->avgPoint=$mathPoints;
}
inside your blade :
{{ $student->avgPoint}}
Point::where('student_id', $student->id)->get()
When you use all() with Eloquent, it makes the query for you. If you use where(), you have to ‘get’ it using get() before you can use it as a collection.
$mathPoints = Point::where('student_id', $student->id)
->get()
->pluck('points');
However, I’d probably look at using more complicated queries to fetch this data, as you could easily end up making hundreds of queries per page, rather than just 1.

A correct query to get data from 3 tables and show it as a single element in laravel?

Suppose I have 3 tables, posts, post_images, and post_links.
post.id is a foreign key in both post_images and post_links.
Each post have multiple images.
I need a data which contains post, its images and its links as single element/array item. If there are 3 posts, I need 3 arrays with each array containing the posts images and links.
My code so far,
$data = DB::table('posts')
->join('post_images','posts.id' ,'=', 'post_images.post_id')
->join('post_links','posts.id' ,'=', 'post_links.post_id')
->select('posts.*')
->get();
with the above query I am getting all the records joined, If i have 3 records with 3 images each, I am getting 9 records, I just need 3 posts with its data as its sub arrays.
Any suggestion?
Here is the PostImage model
class PostImage extends Model
{
public function post() {
return $this->belongsTo(Post::class);
}
}
Here is the PostLink model
class PostLink extends Model
{
public function post() {
return $this->belongsTo(Post::class);
}
}
Here is the Post model
class Post extends Model
{
public function links() {
return $this->hasMany(PostLink::class);
}
public function images() {
return $this->hasMany(PostImage::class);
}
}
In the view you can reach everything you need.
#foreach ($posts as $post)
{$post->title} <br>
#foreach ($post->links as $link)
{$link->url} <br>
#endforeach
#foreach ($post->images as $image)
{$image->src} <br>
#endforeach
#endforeach
And if you want use less queries you could use eager loading to fetch all this data the first time. Eager Loading Laravel
Should look something like this
$posts = Post::with('images','links')->get();
if you already have relation in model you just have to use with method like
$data = PostModel::with('post_images','post_links')->get();
make it dd($data) and look at this. hope it will work.
References: https://laravel.com/docs/5.4/eloquent-relationships#eager-loading

Combining query results in laravel

On my website, users can upload images and attach tags to those images.
I've got an images table,a tag table and an images_tag pivot table.
Images can have many tags, and tags can belong to many images.
I want to be able to generate a list of all the tags a user has used in his/her images.
$imageIDs = Images::where('created_by', Auth::user()->id)->lists('id');
So this would create a list of all the image IDs that a user has upload.
What I want is essentially "foreach $imageIDs, check the images_tag table and for every match go to the tags table and get me back the tagname value."
But I have no idea how I'd do that.
Maybe a foreach then use the merge method on all the results? Any help would be appreciated!
You need to use whereHas() to check the relationship:
$userTags = Tags::whereHas('images', function($q) {
$q->where('created_by', auth()->user()->id);
})->get();
Then just pass this data to a view:
return view('some.view', compact('userTags'));
And iterate over tags in a view:
#foreach ($userTags as $tag)
{{ $tag->name }}
#endforeach
What you could do is this.
class Tag extends Model
{
public function images()
{
return $this->belongsToMany(Image::class);
}
}
class SomeController
{
public function someMethod()
{
$tags = Tag::with(['images' => function ($image) {
return $image->where('created_by', Auth::user()->id);
}])->select('id', 'tagname')->get();
// these are your $tags
}
}
You should not use a query inside foreach(). Then it would result N+1 problem. What you instead do is eager loading using with() statement.

Laravel multiple table join saved to a variable and running a foreach against it (search function)

I presently have 3 tables: Shows, Genres and Show_Genre (associates the two). I have a search form that submits a series of checkboxes with values into an array based on what genres they selected. Presently I want to associate the Shows table and the Genres table into a variable and run a query against it once for every genre checkbox selected. Then, once the selection is filtered, I can display the resulting Show objects that matched the users parameters.
My present setup is the following
public function searchShows(SearchRequest $request)
{
//$ShowsWithGenres give a collection inside a collection which I can't seem to even access its values without doing a bunch of ugly code using count() in a for loop
$ShowsWithGenres = Show::with('genres')->get();
$genres = $request->name;
if(isset($genres))
{
foreach($genres as $genre)
{
//iterate against the selection repeatedly reducing the number of results
}
}
}
Thanks.
You should use whereHas() and whereIn.
Perhaps something like this should do it:
$shows = Show::whereHas('genres', function($q) use($genre_ids)
{
$q->whereIn('id', $genre_ids);
})->get();
EDIT
Try this, however I'm unsure about the performance.
$query= Show::query();
foreach($genre_ids as $id){
$query->whereHas('genres', function($q) use($id)
{
$q->where('id', $id);
})
}
$shows = $query->get();
Using Eloquents whereHas() function you can query results based on the relation's data. http://laravel.com/docs/5.0/eloquent#querying-relations
public function searchShows(SearchRequest $request)
{
// Start a query (but don't execute it at this point as we may want to add more)
$query = Show::with('genres');
$genreNames = (array) $request->name;
// Check there are some genre names, if theres not any it'll cause an SQL syntax error
if (count($genreNames) > 0)
{
$query->whereHas('genres', function($subQuery) use ($genreNames)
{
$subQuery->whereIn('genre_name', $genreNames);
}
}
// Finally execute the query. $shows now contains only shows with the genres that the user has searched for, if they didn't search with any genres, then it contains all the results.
$shows = $query->get();
}

Categories