Search code examples
phplaravel

How to filter the output of the data received from the database based on the meta relation?


I want to filter the output of the data I receive from the database based on the meta relation. The output looks like this:

  #primaryKey: "ID"
  #keyType: "int"
  +incrementing: true
  #with: array:1 [▶]
  #withCount: []
  +preventsLazyLoading: false
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #escapeWhenCastingToString: false
  #attributes: array:23 [▶]
  ...
  #relations: array:1 [▼
    "meta" => \MetaCollection {#2861 ▼
      #items: array:58 [▼
        0 => \PostMeta {#3604 ▼
          ..
          #attributes: array:4 [▼
            "meta_id" => 892
            "post_id" => 253
            "meta_key" => "_manage_stock"
            "meta_value" => "yes"
          ]
          ...
        }

Inside the meta relation, I only want to have the item where the meta_key is equal to _manage_stock. The code I wrote does not work correctly and doesn't apply any filters to it.

$metaKey = '_manage_stock';

$filteredProducts = Product::all()
    ->filter(function ($product) use ($metaKey) {
        return $product->meta->contains(function ($meta) use ($metaKey) {
            return $meta->meta_key === $metaKey;
        });
    });

This structure and data are related to WordPress, and for some reasons, I cannot use whereHas. How can I extract meta_key values from the meta relationship where the value is equal to _manage_stock?


Solution

  • If you want to filter at the query level, use whereHas() to apply the condition directly to the database query.

    $metaKey = '_manage_stock';
    
    $filteredProducts = Product::whereHas('meta', function ($query) use ($metaKey) {
        $query->where('meta_key', $metaKey);
    })->get();