1

i am using Metable in my project for creating meta for orders
but i have one problem
i want group orders that have same email
this is metable table image : image 1 image 2

i need code like this can work :)

Order::whereHas( 'meta', function($query) {
        $query->where("key" , "gmail");
})->groupBy('meta.value')->get();

and this is meta relation that called by trait 'use Metable' in Order Model:

public function meta(): MorphMany
{
    return $this->morphMany($this->getMetaClassName(), 'metable');
}

Thanks

Dr Web
  • 13
  • 4

3 Answers3

1

This query may work, we start querying from the Meta model:

Meta::with('metable')
    ->whereHasMorph('metable', Order::class)
    ->where('key', 'gmail')
    ->get()
    ->groupBy('value')
    ->map(function ($metaCollection) {
        return $metaCollection->map->metable->unique('id');
    });
Kevin Bui
  • 2,754
  • 1
  • 14
  • 15
0

This is untested, but I would give it a try to retrive the models from the Order class:

Order::whereHas('meta', function($query) {
    $query->where('key', 'gmail');
})->with(['meta' => function($query) {
    $query->groupBy('value')
}])->get();
mdexp
  • 3,492
  • 2
  • 9
  • 20
0

Try this solution instead:

Order::whereHas( 'meta', function($query) {
    $query->where("key" , "gmail")
    $query->groupBy('meta.value');
})->get();
sud007
  • 5,824
  • 4
  • 56
  • 63
Chinnu
  • 61
  • 8