GithubHelp home page GithubHelp logo

Comments (13)

Tucker-Eric avatar Tucker-Eric commented on May 14, 2024 1

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.

Tucker-Eric avatar Tucker-Eric commented on May 14, 2024 1

Awesome! I'll open an issue in Laravel/send a PR to patch the issue

from eloquentfilter.

Tucker-Eric avatar Tucker-Eric commented on May 14, 2024

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.

techdaddies-kevin avatar techdaddies-kevin commented on May 14, 2024

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.

Tucker-Eric avatar Tucker-Eric commented on May 14, 2024

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.

techdaddies-kevin avatar techdaddies-kevin commented on May 14, 2024

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.

techdaddies-kevin avatar techdaddies-kevin commented on May 14, 2024

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.

techdaddies-kevin avatar techdaddies-kevin commented on May 14, 2024

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.

Tucker-Eric avatar Tucker-Eric commented on May 14, 2024

What version of Laravel and laravel-medialiabrary are you using?

from eloquentfilter.

techdaddies-kevin avatar techdaddies-kevin commented on May 14, 2024

7.1.3 medialibrary
5.6.16 laravel

from eloquentfilter.

Tucker-Eric avatar Tucker-Eric commented on May 14, 2024

Looking into this, I think it may be a Laravel issue... Do you have $withCount = ['media'] on your Listing model?

from eloquentfilter.

techdaddies-kevin avatar techdaddies-kevin commented on May 14, 2024

Yes indeed.

from eloquentfilter.

techdaddies-kevin avatar techdaddies-kevin commented on May 14, 2024

Confirmed. Adding a global scope and removing the withCounts property did fix this issue.

from eloquentfilter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.