Comments (13)
Then it definitely looks like a Laravel issue. If you removed that and added it as a global scope instead of a property, it should fix this issue.
What is going on is that in the nested where()
laravel is calling newQueryWithoutScopes
on the builder's model which loads that withCount
in the nested query, registering the bindings for the withCount
query so those get loaded into the subquery without the actual constraint which is where the bindings are getting shifted.
from eloquentfilter.
Awesome! I'll open an issue in Laravel/send a PR to patch the issue
from eloquentfilter.
What does the method calling filter()
look like?
Also, if you're passing anymore input to filter()
besides ['search' => 'a']
, can you provide that too?
And it may also help to see the media
relationship definition on your Listing
model
from eloquentfilter.
The call to filter() is $listings = Listing::filter($request->all())->with('manufacturer', 'media')->simplePaginateFilter(5);
The only other filters are for two foreign keys, and I'm just passing the ID so nothing complicated there
public function category($id)
{
return $this->where('category_id', $id);
}
public function manufacturer($id)
{
return $this->where('manufacturer_id', $id);
}
public function search($search)
{
return $this->where(function($q) use ($search)
{
return $q->where('description', 'LIKE', "%$search%")
->orWhere('title', 'LIKE', "%$search%");
});
}
The media relationship comes from the medialibrary package, but I've removed that entirely from the filter call and still experience the same result.
from eloquentfilter.
I have a feeling there may be something in your media
relation on your Listing
model. From what your sql prints out from your query it looks like there is an extra App\Listing
being pushed to the bindings array of the query, causing all the rest of the parameters to be shifted forward by one.
From what it is printing out, I am assuming the input passed to filter
is:
[
'search' => 'a',
'category' => 2
];
Because from what that sql shows, the limit should be 5 from your paginator call, not 2 and if you remove the App\Listing
from the nested where()
in your search()
method and shift all parameters left by one, the query starts to take shape.
from eloquentfilter.
The pagination param issue is just because I changed that value in the code between my first post and second here. Right now pagination is set to 5 per page and the query is:
select `listings`.*, (select count(*) from `media` where `listings`.`id` = `media`.`model_id` and `media`.`model_type` = 'App\Listing') as `media_count` from `listings` where (`description` LIKE 'App\Listing' or `title` LIKE '%a%') limit 6 offset 0
However, you're definitely right. Something is clashing between this code and the medialibrary code. If I remove the medialibrary interface and trait from my Listing model, the generated query is correct.
from eloquentfilter.
For what it's worth, I just added a global scope to the Listing model to only show results with a renewal date in the future. When I did that, the query changed to:
select `listings`.*, (select count(*) from `media` where `listings`.`id` = `media`.`model_id` and `media`.`model_type` = 'App\Listing') as `media_count` from `listings` where (`description` LIKE 'App\Listing' or `title` LIKE '%a%') and `renewal_date` > '%a%' limit 6 offset 0
The problem is definitely that App\Listing is for some reason in the stack twice.
from eloquentfilter.
Sorry for the barrage of updates. If I run the search without any filters, this is the query generated with my new global scope in place:
select `listings`.*, (select count(*) from `media` where `listings`.`id` = `media`.`model_id` and `media`.`model_type` = 'App\Listing') as `media_count` from `listings` where `renewal_date` > '2018-04-19 17:36:13' limit 6 offset 0
As you can see, the date is correct and not "App\Listing". Additionally, if I run through my filters (category and manufacturer) WITHOUT specifying a search keyword, the query is correct. It is something specifically happening when using the where method INSIDE the anonymous function. If I simply use a
public function search($search)
{
return $this->where('description', 'LIKE', "%$search%");
}
It works fine. But as soon as I do:
public function search($search)
{
return $this->where(function($q) use ($search)
{
return $q->where('description', 'LIKE', "%$search%");
});
}
It screws up.
from eloquentfilter.
What version of Laravel and laravel-medialiabrary are you using?
from eloquentfilter.
7.1.3 medialibrary
5.6.16 laravel
from eloquentfilter.
Looking into this, I think it may be a Laravel issue... Do you have $withCount = ['media']
on your Listing
model?
from eloquentfilter.
Yes indeed.
from eloquentfilter.
Confirmed. Adding a global scope and removing the withCounts property did fix this issue.
from eloquentfilter.
Related Issues (20)
- Combine filter by related and own fields HOT 4
- Postgresql support (case-insensitively) HOT 2
- Filter on append attributes HOT 1
- sorted by table with relation HOT 1
- Breaking change on 2.4.1 from 2.4.0 HOT 3
- Add orWhereLike HOT 2
- How should you handle Laravel Scout integration? HOT 8
- filter by created_at HOT 1
- Your composer is powerful! HOT 1
- Access the other request parameters within the filter function? HOT 2
- Access model class HOT 2
- filtering by date interval HOT 1
- Filter by column and and relation column HOT 1
- Related filter does not accept zero or false value
- $relations setting to create orWhereHas HOT 4
- Can't sort many to many HOT 1
- Can't sort on date HOT 1
- Filter by relationship field and latest fails HOT 3
- Relations with one related method HOT 3
- Filtering by deep relation HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from eloquentfilter.