Navigation Menu

Skip to content
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

Filtering #4

Closed
it-can opened this issue Mar 7, 2017 · 9 comments
Closed

Filtering #4

it-can opened this issue Mar 7, 2017 · 9 comments
Labels

Comments

@it-can
Copy link

it-can commented Mar 7, 2017

Hi I am using your package for a sort of webshop with a couple of products. The products have multiple meta values. Is there a easy way to create some sort of product meta filtering?
Something like this:

Width:

  • 10mm (4 products)
  • 20mm (1 product)

Height:

  • 100mm (4 products)
  • 200mm (1 products)
@frasmage
Copy link
Collaborator

frasmage commented Mar 7, 2017

Hi @it-can,

Depending on your set up there are a number of ways you could approach this.

You can do the following From the query builder:

$productMeta = Meta::select('key', 'value')->selectRaw('count(*) as count')
->where('meta.metable_type', '=', Product::class);
})->groupBy(['key', 'value'])
->get();

// returns a collection of records, which each have a key, value and count attribute
// loop as necessary for your needs.

Assuming you have all products in a collection:

$counts_by_width = $products->groupBy(function($product){
    return $product->getMeta('width', null);
})->map(function($group){
    return count($group);
});

// outputs [ '10mm' => 4, '20mm' => 1]

Hope this helps

@it-can
Copy link
Author

it-can commented Mar 7, 2017

ok thanks will test it... 👍

@it-can
Copy link
Author

it-can commented Mar 9, 2017

Hey @frasmage, does the metable_type selection work correctly? When running this on my db, it doesn't return results.

select * from `meta` where `meta`.`metable_id` in ('1', '2', '3', '4', '5', '6', '7', '8', '9') and `meta`.`metable_type` = 'App\Models\Product'

but when removing the metable_type it works and gives results...

select * from `meta` where `meta`.`metable_id` in ('1', '2', '3', '4', '5', '6', '7', '8', '9')

@frasmage
Copy link
Collaborator

frasmage commented Mar 9, 2017

Yes, the metable_type is necessary to only get back records that are attached to products, as opposed to any other models that you have applied meta to.

I'm not able to tell what is misconfigured from your SQL example. Could you show me the code executing this and/or a few example rows from your DB?

@it-can
Copy link
Author

it-can commented Mar 9, 2017

It needs a double slash then it works...

@frasmage
Copy link
Collaborator

frasmage commented Mar 9, 2017

@it-can right.

Are you writing the query yourself or through the query builder, as above? The query builder should handle that automatically.

@it-can
Copy link
Author

it-can commented Mar 9, 2017

Yeah it works, I was trying the query in phpmyadmin for testing

@frasmage
Copy link
Collaborator

@it-can Cool. Anything else, or shall I close this issue?

@it-can
Copy link
Author

it-can commented Mar 10, 2017

Close it... thanks for the help!

@it-can it-can closed this as completed Mar 10, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants