-
Notifications
You must be signed in to change notification settings - Fork 11.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[5.4] Model->where loses param if used with withCount(...) and ->select(..) #19388
Comments
Since you're using the The above works for me without the select (though I used |
Kindly close this issue |
Copying @mcbanderson
Description:I recently upgraded my application to 5.4 from 5.2, and as soon as I finished upgrading I started running into this issue. I have 2 custom models, Event and Gift, which have a one to many relationship set up. Before the upgrade everything worked as expected, but after the upgrade any queries that includes ->withCount('gifts') are not working properly. Some queries, such as the ones below, return an empty Eloquent Collection or null respectively (empty collection for ->get(), null for find(74)). For this example an id of 74 is used, which has been confirmed to exist in the database.
Other queries, such as the one below, will produce a non-empty collection, but some of the where clauses appear to be completely ignored. In this example the correct gifts_count is returned with each Event, but Events that are older than 2 days appear in the collection (the where clause appears to be completely ignored).
The relationship appears to be setup correctly, because the query The things that I find very strange are that:
Steps To Reproduce:I am unsure if this issue is reproducible in a stock installation of Laravel 5.4. I'm hoping to avoid having to setup a clean installation of Laravel 5.4 and manually port my application over, but if I'm unable to resolve this problem in the next day or two I think that will be my next course of action. If there is any additional information I can provide please let me know, and any guidance on possible solutions to try would be greatly appreciated. |
@masterix21 Were you ever able to find a solution to this issue? |
I manually ported my application to a fresh installation of Laravel 5.4, but the issue persists. If anyone has suggestions on what I could try in order to ferret out the cause of the issue, I would love to hear them. In the mean time I think I will need to revert back to an older version of Laravel so I can avoid this issue. |
I reverted to Laravel 5.3.31, which resolved the issue. After reverting to my old commit, I tested the problem queries before and after running |
Is this still an issue? |
Closing this issue because it's already solved, old or not relevant anymore. Feel free to reply if you're still experiencing this issue. |
Description:
I'm trying to count all customer's contacts (CustomerContacts model) with phone number, but it fails because the model (Customer) loses relationship with company_id (Company model) if you use it with method withCount(..). See the example below.
Steps To Reproduce:
$customers = \App\Customer::where('company_id', '=', 4)->withCount(['contacts' => function($query) { $query->where('phone_no', '!=', ''); }])->select(\DB::raw('customers.*'))->paginate(5);
Query log:
select count() as aggregate from
customers
wherecompany_id
= '4';select customers. from
customers
wherecompany_id
= '' limit 5 offset 0;The text was updated successfully, but these errors were encountered: