weaver
weaver

Reputation: 453

Filter query in Meta table Laravel

users Tabel

**id|name**

   1|xx

users_meta Table

id | user_id | metaKey | metavalue

  1 | 1        | city     | kolkata

  2 |2         |city      | london

  3 |8         |city      |london

My moto is to return users belongs to city London.How can i achive this??

Upvotes: 0

Views: 319

Answers (2)

FULL STACK DEV
FULL STACK DEV

Reputation: 15951

Lets say u have a relationship with Users and user_meta

Than simply

User::with(['userMeta' => function($query){
  $query->where('metaKey', 'city')
  $query->where('metavalue', 'london')
}]);

Hope this helps

Upvotes: 1

Brian Lee
Brian Lee

Reputation: 18187

You can query using whereHas:

User::whereHas('users_meta', function ($query) {
    $query->where('metavalue', 'london');
})->get();

Upvotes: 1

Related Questions