This is my current code, when executed will return no results. (Query1)
// Get all product categories that has a relation
$product_categories = ProductCategory::whereExists(function($query) {
$query->select('id')
->from('restaurants')
->whereJsonContains('restaurants.restaurant_categories', 'product_categories.name');
})->get();
// Log
dd($product_categories->toSql());
Here an sql query dump
select *
from `product_categories`
where exists (
select `id`
from `restaurants`
where json_contains(`restaurants`.`restaurant_categories`, ?)
)
and `product_categories`.`deleted_at` is null
While this when executed will return a result (Query2)
// Get all product categories that has a relation
$product_categories = ProductCategory::whereExists(function($query) {
$query->select('id')
->from('restaurants')
->whereJsonContains('restaurants.restaurant_categories', 'Food');
})->get();
// Log
dd($product_categories->toSql());
Here is an sql query dump as well
select *
from `product_categories`
where exists (
select `id`
from `restaurants`
where json_contains(`restaurants`.`restaurant_categories`, ?)
)
and `product_categories`.`deleted_at` is null"
table: restaurants
id | name | restaurant_categories |
---|---|---|
1 | fancy | ["Food"] |
table: product_categories
id | name | type |
---|---|---|
1 | Food | fragile |
This is my updated code, when executed will return no results. (Query3)
// Get all product categories that has a relation
$product_categories = ProductCategory::whereExists(function($query) {
$query->select('id')
->from('restaurants')
->whereJsonContains('restaurants.restaurant_categories', \DB::raw('product_categories.name'));
})->get();
// Log
dd($product_categories->toSql());
Here is an sql query dump for Query3
select *
from `product_categories`
where exists (
select `id`
from `restaurants`
where json_contains(`restaurants`.`restaurant_categories`, product_categories.name)
)
and `product_categories`.`deleted_at` is null"
the expected query:
select *
from `product_categories`
where exists (
select `id`
from `restaurants`
where JSON_CONTAINS(restaurants.restaurant_categories, CONCAT('"',`product_categories`.`name`,'"'))
);
laravel implementation:
$product_categories = ProductCategory::whereExists(function($query) {
$query->select('id')
->from('restaurants')
->whereJsonContains('restaurants.restaurant_categories', DB::raw('CONCAT(\'"\',`product_categories`.`name`,\'"\')'));
})->get();