Using withCount() on a relationship that has distinct() - laravel

I have the following models:
Question: [title, user_id]
Reply: [body, question_id, user_id]
User: [name]
As you can see, a question has many replies, and a reply belongs to a user.
I've added a contributors relationship to the Question model that retrieves all the users who've added a reply (using the replies as the join table):
public function contributors()
{
return $this->belongsToMany(User::class, 'replies')->distinct('user_id');
}
I had to use distinct() to remove duplicates because a user might post many replies on a single question and this works fine.
Now the problem happens when I do:
Question::withCount('contributors')->get()
It ignores the call to distinct() and gives me the total number of users who've added a reply (including duplicates).
Any idea how I can fix this?

Remove distinct() from the relationship and use withCount() with a raw expression:
public function contributors()
{
return $this->belongsToMany(User::class, 'replies');
}
Question::withCount(['contributors' => function ($query) {
$query->select(DB::raw('count(distinct(user_id))'));
}])->get();

u can also use variable here for distinction
public function contributors()
{
return $this->belongsToMany(User::class, 'replies');
}
Question::withCount(['contributors' => function ($query) use ($var) {
$query->select(DB::raw('count(distinct(user_id))'));
}])->get();

You need to continue using the distinct('user_id') in the relationship method for operations like these to work correctly:
$question->contributors()->count();
$question->contributors->count();
$question->contributors()->get()->count();
otherwise in some cases you will get different results.

Related

Sort the results returned by model relation laravel

I have 2 table:
products: id, name
auctions: id, product_id, expired_time
Model product
function getAuctions(){
return $this->hasOne('App\Models\Auction', 'product_id', 'id');
}
How can I do the same as below(auctions.expired_time not working):
Controller
function getProductAuctions(){
return \App\Models\Product::with('getAuctions')->whereHas('getAuctions', function(){})->orderBy('auctions.expired_time', 'asc');
}
Thanks everyone.
You can do it with sortBy() method. That method applies to collections only.
function getProductAuctions(){
$products = \App\Models\Product::all();
return $products->sortBy(function ($product, $key) {
return $product->getAuctions->expired_time;
})
}
This should now sort your products collection by the auctions expired time, if you want reversed order, you can just use sortByDesc() method, rest is the same. Also, in your product model, if you are having hasOne() relationship, rename the function name to getAuction() or even better auction() (singular).
you can use one of these to get sort your result
in your controller
function getProductAuctions(){
return \App\Models\Product::with('getAuctions')->whereHas('getAuctions', function($query){
$query->orderBy('expired_time', 'asc');
})->get();
}
and if you want to sort in your model then just use this
function getAuctions(){
return $this->hasOne('App\Models\Auction', 'product_id', 'id')->orderBy('expired_time', 'asc');
}
and thats all no need to do any thing...your result is already sorted
Thank everyone. I found the answer here and I find it correct: https://laracasts.com/discuss/channels/eloquent/order-by-on-relationship
"When you use the with method the Eloquent makes another query in database to retrieve the related data using whereIn. This is why we can't order by a relation field."

Eloquent Removing Columns

I'm having a really strange issue with my eloquent query. I have a table called Calls which I am joining to Contacts and Companies. I am trying to reference the column calls.id but it has been replaced with the id for Companies.
Here is my query:
$calls=DB::table('calls')
->leftJoin('contacts','calls.contact_id','=','contacts.id')
->leftJoin('companies','calls.company_id','=','companies.id')
->where('completed','=',false)
->orderBy('call_on','asc')
->get();
return $calls;
I have seen on Github that this seems to be a known bug but no-one has put forward a workaround.
Can anyone point me in the right direction?
The most direction solution to your immediate question is to add a select to your Eloquent query:
$calls=DB::select('calls.* from calls')
->leftJoin('contacts','calls.contact_id','=','contacts.id')
->leftJoin('companies','calls.company_id','=','companies.id')
->where('completed','=',false)
->orderBy('call_on','asc')
->get();
return $calls;
Instead of the default select *, explicitly dictate what is returned. However, this can be done a lot more cleanly with Eloquent using models:
Calls::whereHas('companies', function (Builder $query) {
$query->where('completed', false);
})->orderBy('call_on', 'asc')->get();
In order for this to work you need to setup the relationship on the model level:
// App\Calls model:
public function companies() {
return $this->belongsTo(App\Companies::class);
}
// App\Companies model:
public function calls() {
return $this->hasMany(App\Calls::class);
}

How to use where condition in laravel eloquent

I am using laravel eloquent. I have fetched data from two table using eloquent.
I have post table and chat table. For post table I have model Post.php and for chat table I have model Chat.php. Here is the the eloquent relation I have created to fetch chat for individual post for a user.
in Post.php
public function TeamMessage()
{
return $this->hasMany('App\Chat','post_id');
}
And in Chat.php
public function ChatRelation()
{
return $this->belongsTo('App\Post');
}
it is working perfect. But this relation fetch all messages for a specific post. I want to fetch all unread message from chat table. I have a column named unread in chat table.
Now my question is how I can fetch only unread message for a specific post.
While the other answers all work, they either depend on scopes (which are very useful in many circumstances) or on you having already instantiated an instance of $post, which doesn't let you eager load multiple posts with their messages.
The dynamic solution is this, which will let you fetch either 1 or more posts and eager load their messages with subquery:
$posts = Post::with(['TeamMessage' => function ($query) {
$query->where('unread', true); // This part applies to the TeamMessage query
}])->get();
See in documentation
Edit:
If you, however, want to filter the posts, to only show those that have unread messages, you need to use whereHas instead of with:
$posts = Post::whereHas(['TeamMessage' => function ($query) {
$query->where('unread', true); // This part applies to the TeamMessage query
}])->get();
More in the documentation.
You can also chain whereHas(...) with with(...).
For querying relationships, you have to call them as functions instead of properties, like this:
$unreadPosts = $post->TeamMessage()->where('unread', true)->get();
For more information on this you can take a look at the docs.
You need to create a local scope on your model, information on local scopes can be found here: https://laravel.com/docs/5.6/eloquent#local-scopes
public function scopeUnread($query)
{
return $query->where('unread', 1);
}
Then in your controller/view
$unread = $yourmodel->unread()
First I would change your relation names to the name of the entity in lower case:
in Post.php
public function chats()
{
return $this->hasMany('App\Chat','post_id');
}
And in Chat.php
public function post()
{
return $this->belongsTo('App\Post');
}
public function scopeUnread($query)
{
return $query->where('unread', 1);
}
Then you can use
$post->chats()->unread()->get();

Laravel 5, get only relationship for eloquent model

I have a user and article models in my app. The relation between them is straightforward:
//article.php
use Taggable;
public function user()
{
return $this->belongsTo('App\User');
}
Article uses the Taggable lib which provides me with a variety of methods like Article::withAnyTags (returns all articles tagged with 'xyz' string). Now I'd like to get all users who posted an article/s tagged as 'xyz'. I know how to do this in two lines but something tells me that this is not right. Ideally I'd like to do sth like:
$users = Article::withAnyTags('xyz')->with('user')->select('user'); --pseudocode
Is it possible to do sth like this in eloquent?
Side note: I knot that I could do this with DB::table but this is not an option for me. Please note that there is no "->get()" at the end on my pseudocode. It's so, because I'm paginating the users' results set with lib which works only with eloquent queries.
You could use whereHas():
User::whereHas('articles', function ($query) {
$query->withAnyTags('xyz');
})->paginate();
Or if you need to pass a variable of the tags to the closure you could do:
$tag = 'xyz';
User::whereHas('articles', function ($query) use($tag) {
$query->withAnyTags($tag);
})->paginate();

Laravel Relationships Conditions - 3 tables

I've got a situation where I've got Posts, Users and Comments.
Each comment stores a post_id and a user_id. What I want to do is get all of a user's comments on a particular post, so that I can do a call like this:
$comments = Auth::User()->comments(post_id=x)->text
(where I know what x is)
I have:
User->HasMany(comments)
Comments->HasOne(User)
Comments->HasOne(Project)
Project->HasMany(comments)
I feel like there needs to be a where or a has or a wherehas or something thrown in.. the best I can manage is that I pull Auth::User()->comments into an array and then search through the array until I find the matching post ID.. that seems wasteful.
with doesn't apply any join, so you can't reference other table.
You can use this:
// User model
public function comments()
{
return $this->hasMany('Comment');
}
// Comment model
public function scopeForPost($query, $postId)
{
$query->where('post_id', $postId);
}
// then you can do this:
Auth::user()->comments()->forPost($postId)->get();
Alternatively you can eager load comments with constraint:
User::with(['comments' => function ($q) use ($postId) {
$q->where('post_id', $postId);
}])->find($someUserId);
// or exactly the same as above, but for already fetched user:
// $user .. or
Auth::user()->load(['comments' => function ($q) use ($postId) {
$q->where('post_id', $postId);
}]);
// then you can access comments for $postId just like this:
Auth::user()->comments; // collection
When you need to filter your relations, you just have to do it in your Eloquent query:
$data = User::with('posts', 'comments')
->where('users.id', Auth::User()->id)
->where('posts.id', $postID)
->get();
Then you can
foreach($data->comments as $comment)
{
echo $comment->text;
}
Your Comments table would have foreign keys Post_Id and User_ID
To Access all the comments of a particular post from a particular user , can you try this way?
Comment::select('comments.*')
->where('comments.user_id', Auth::user()->id)
->leftJoin('posts','posts.id','=','comments.post_id')
->leftJoin('users','users.id','=','comments.user_id')
->get();
Am sure there is better way to achieve it, but this should give you desired results.
Note use aliases if you have conflicting column names
Let me know if this worked.

Resources