Making a query including model's method - laravel

I've been making a query to return all those people who have answered all the questions in a certain block. I've been successfull in getting all the users who answered like this:
public function getUsers()
{
$question_ids = $this->questions->pluck('id');
return User::query()->whereHas('question_answers', function ($query) use ($question_ids) {
$query->whereIn('question_id', $question_ids);
})->get();
}
And I have a function to check if a certain user has answered all the questions in a certain block.
public function hasAnsweredAll(User $user)
{
return (boolean) ($this->questions->count() == $user->question_answers()->whereHas('question', function($query){
$query->where('question_block_id', $this->id);
})->count());
}
Now I get the users I want like the following:
$users=[];
foreach($question_block->getUsers() as $user){
if($question_block->hasAnsweredAll($user)){
$users[] = $user;
}
};
Now my question is, instead of foreach, can I involve my hasAnsweredAll() method in a whereHas() or can I avoid somehow using foreach? I've searched the net for hours now, but no luck, so any help is appreciated.

You can take a look to this article from Jonathan Reinink, he uses subqueries to solve a problem like the one you mentioned
I hope this works for you

I've managed to work out a solution for my problem. It is so easy, and I've missed this so far when reading the documentations. whereHas() can take an operation and an int as parameters, so it will return instances according to that.
So like this:
public function getUsers()
{
$question_ids = $this->questions->pluck('id');
return User::query()->whereHas('question_answers', function ($query) use ($question_ids) {
$query->whereIn('question_id', $question_ids);
}, '=', $this->questions->count())->get();
}
I don't even need the hasAnsweredAll method anymore, because only users who answered all are returned. So instead the foreach it will look like this:
$users = $question_block->getUsers();

Related

Best way to query model relationship in Eloquent

I am working within a controller in a Laravel application. I am returning a table to the view. The table is based on my PlanSubmission model. I am receiving parameters through a GET request and using those parameters to return a filtered set of rows to my view.
The first part of my controller looks like this and is working fine:
public function index()
{
//Used for filter. The request is received in the URL
if (request()->has('status')) {
$plans = PlanSubmission::where('status', request('status'))->paginate(25)->appends('status', request('status'));
}
elseif (request('employer_name')) {
$plans = PlanSubmission::where('employer_name', request('employer_name'))->paginate(25)->appends('employer_name', request('employer_name'));
}
I have run into a problem because now I need to use a model relationship in the controller. I am receiving 'advisor_name' from the request. The 'advisor_id" column is the foreign key on the PlanSubmission model. The 'advisor_name' column exists in the Advisor model. I have a function on my PlanSubmission model that looks like this:
public function advisor()
{
return $this->belongsTo(Advisor::class);
}
Initially, I thought there was a way I could do this easily with something like:
$plans = PlanSubmission::where(advisor->name, request('advisor_name'))->paginate(25)->appends('advisor_name', request('advisor_name'));
Of course, this will not work because I cannot enter a relationship into the first parameter in the Where Clause.
I do not know where to go from here. My other thought is to return all the advisors first from the Advisor model like this:
$advisors = Advisor::where('name', request('advisor_name'));
Then, I imagine I would have to somehow loop through that and get the id (primary key) for each of the objects in $advisors and somehow get that into the PlanSubmission where clause. I'm totally lost.
Like Victor mentions in his answer you can use whereHas like so:
PlanSubmission::whereHas('advisor', function ($query) {
$query->where('name', request('advisor_name'));
});
You didn't asked this directly, but I noticed that you use conditionals to make different queries. Eloquent provides a few way to make this a bit nicer to deal with.
The first which is kind of obvious is that that whatever method you call a builder (query) is returned that you can just add on to. It could be there were some common restrictions in your two cases:
public function index()
{
$query = PlanSubmission::where('something', 42);
if (request()->has('status')) {
$query = $query->where('status', request('status'));
} elseif (..) {
...
}
return $query->paginate(25);
}
Another way to do conditional queries in Laravel is using when. E.g. for status:
$query = $query->when(request->has('status'), function ($query) {
// note that you don't have to return the query
$query->where('status', request('status'));
});
// or PlanSubmission::>when(..)
In your example you cannot both filter by status AND advisor_name, but lets assume that would be okay, then you can combine everything like so:
public function index()
{
return PlanSubmission::query()
//->where('something', 42)
->when(request->has('status'), function ($query) {
$query->where('status', request('status'));
})
->when(request->has('advisor_name'), function ($query) {
$query->whereHas('advisor', function ($query) {
$query->where('name', request('advisor_name'));
});
})->paginate(25);
}
This approach may seem verbose for simple queries and then it is fine to use if conditions, but for complex queries when can be useful. Also the idea of "building up a query" also works nice in those situation. You can pass the query builder around and continuously build it up.
You can use whereHas for that
docs

Consultation through relationships in laravel

I searched endlessly for a question that answered my question here and I didn't find it. My question is as follows, I have 3 models: User, Post and Comments. Where user has a relationship with one to many posting, and Post has a relationship with one to many comments as well. How can I get all of the user's comments on all posts?
Currently my solution looks like this:
Models Users:
public function comments(){
$comments = array();
foreach ($this->posts()->get() as $el) {
foreach ($el->posts()->get() as $nEl) {
array_push($comments, $nEl);
}
}
return collect($comments);
}
I would like a less expensive and native solution for laravel, if any.
On your User model, something like:
public function getComments()
{
return Comment::whereHas('posts', function ($query) {
$query->where('user_id', $this->id);
})->get();
}
Also see:
https://laravel.com/docs/6.x/eloquent-relationships#has-many-through

Laravel : Get all models that their last relationship have some condition

I have two models Post and Comment, i'd like to get all posts that their last comment is active:
// Model Post
public function comments()
{
return $this->hasMany('comments');
}
//Model Comment
public function post()
{
return $this->belongsTo('post');
}
i tried this solution :
public function lastComment()
{
return $this->hasOne('comment')->latest()
}
and in my controller :
$postsWithLastActiveComment = Post::whereHas('lastComment', function($q){
$q->where('active',1);
})->all();
but in this solution if the last comment is not active Previous comment will be taken
I am not sure if there's another simpler way of doing this, but maybe you can try it with a sub-query?
$lastComment = Comment::select('active')
->whereColumn('post_id', 'posts.id')
->latest()
->limit(1)
->getQuery();
$posts = Post::select('posts.*')
->selectSub($lastComment, 'last_comment_is_active')
->having('last_comment_is_active', 1)
->get();
->latest() only orders the posts by created_at so to get only the latest comment you need ->latest()->first()
I think the code below should work !
public function comments()
{
return $this->hasMany('comments');
}
public function lastComment()
{
return $this->comments()->latest()->first();
}
Shouldn't this
$postsWithLastActiveComment = Post::whereHas('lastComment', function($q){
$q->where('active',1);
})->all();
be
$postsWithLastActiveComment = Post::whereHas('lastComment', function($q){
$q->where('active',1);
})->get();
According to your question, the Post model has many Comments. And you want to get the comment from the post where active is one and must be the lastest id.
Get the last comment like the following
public function lastComment()
{
return $this->hasOne('comment')->latest()->take(1);
}
Get all posts which had the lastComment like the following
$latestCommentPosts = Post::whereHas('lastComment')->get()
And filter the latestCommentPosts like the following
$latestCommentPosts->where('active', 1)->get()
Or, you can archive by one query like the following as well.
Post::whereHas('comments', function($q) {
$q->where('active', 1);
})->get()
Like that, you got all the latest comment with active is 1.

How to sort a query by the results of a model's method in Laravel

I have a model that has a "hasMany" relationship with a second model which records "votes" for the former. I then have a method which can be called upon to simply count the number of votes, as follows:
class Car extends Model
{
public function votes()
{
return $this->hasMany(Vote::class);
}
public function score()
{
return $this->votes->count();
}
}
In my controller, I want to run a query that returns all() but sorts by score()... something along the lines of:
$cars = Car::all()->sortBy('score');
But that doesn't work obviously... I feel like I'm on the right track, but don't quite know how to make this work. Would appreciate some help. Thanks.
Try this solution:
$cars = Car::with('votes')->get()->sortBy(function($car)
{
return $car->votes->count();
});
Hope it help you :)
the code should be like this :
$products = Shop\Product::join('shop_products_options as po', 'po.product_id', '=', 'products.id')
->orderBy('po.pinned', 'desc')
->select('products.*') // just to avoid fetching anything from joined table
->with('options') // if you need options data anyway
->paginate(5);

Problems with eagerly loading constraints in Laravel 4 - a workaround?

I have 2 tables, Places and Users. Places can have many Users.
// in Place.php
public function users()
{
return $this->hasMany('User');
}
//in User.php
public function place()
{
return $this->belongsTo('Place');
}
and am trying to get only the Places that have at least one active and enabled User associated through a static method Place::locationsWithPeople().
public static function locationsWithPeople()
{
return Place::with(array('users' => function($query)
{
$query->where('enabled', '=', 1)->where('active', '=', 1);
}))->get();
}
This yields an HTTP 500. Removing the two wheres does not help.
This works, but of course it does not contain the two wheres:
return Place::has('users')->get();
Anyone can help? It seems totally analogous to the example in Laravel's documentation.
This works for me.
$place = new Place;
$array = $place->with(array('users' => function($query)
{
$query->where('enabled', 1)->where('active', 1);
}))->get();
var_dump($array->toArray());
Maybe because you are trying to use non-static methods statically?
It seems that the problem was caused by excessive resource consumption, leading the page to HTTP500 over resource exhaustion. Fixed by rewriting it through join statements.
Can you try this?
return Place::with(array('users' => function($query)
{
$query->where('enabled', '=', 1)->where('active', '=', 1);
}))->hasWith('users')->get();
Method hasWithis a scope that written by me. You can get scopeHasWith() from http://paste.laravel.com/13jb. This will work same as has() method but it will call eagerload closure before attach WHERE to query builder.
I think this can resolve your problem that used resource too much and you can WHERE them from eagerload closure. I had same problem as you that I want to query with relation but it send all records that match WHEREIN too and cause memory problem.

Resources