Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel雄辩地铸造了数组字段拉取结果,其中包含一些提交的ID_Php_Laravel_Eloquent_Laravel 6 - Fatal编程技术网

Php Laravel雄辩地铸造了数组字段拉取结果,其中包含一些提交的ID

Php Laravel雄辩地铸造了数组字段拉取结果,其中包含一些提交的ID,php,laravel,eloquent,laravel-6,Php,Laravel,Eloquent,Laravel 6,我有一个名为recipes的表,它将一个字段强制转换为一个数组,该数组包含来自另一个表配料的ID。用户可以从表单中选择他们想要查看的配方的特定成分 有没有一种方法可以让我获取已提交的ID列表,并且只提取包含其中一些ID的食谱?我说一些是因为我不想局限于只有所有ID的食谱。这意味着,如果有人选择鸡肉、洋葱和芹菜,并且有包含这些成分的食谱,再加上胡萝卜、辣椒和蘑菇,我也想展示一下 我能提出的唯一问题似乎是比较精确 $recipes = Recipes::whereIn('ingredient_ids

我有一个名为
recipes
的表,它将一个字段强制转换为一个数组,该数组包含来自另一个表
配料的ID
。用户可以从表单中选择他们想要查看的配方的特定成分

有没有一种方法可以让我获取已提交的ID列表,并且只提取包含其中一些ID的食谱?我说一些是因为我不想局限于只有所有ID的食谱。这意味着,如果有人选择鸡肉、洋葱和芹菜,并且有包含这些成分的食谱,再加上胡萝卜、辣椒和蘑菇,我也想展示一下

我能提出的唯一问题似乎是比较精确

$recipes = Recipes::whereIn('ingredient_ids', $request->ingredient_ids)->get();

使用
component\u id
作为提交的成分列表。

我知道您将字段转换为
数组
,但这可能会起作用:如果您的数据库是MySQL或PostgreSQL,您可以使用

从文档中:

MySQL和PostgreSQL支持
whereJsonContains
具有多个值:

$users = DB::table('users')
            ->whereJsonContains('options->languages', ['en', 'de'])
            ->get();
因此,请在代码中尝试以下操作:

$recipes = Recipes
       ::whereJsonContains('ingredient_ids', $request->ingredient_ids)
       ->get();

可能值得创建一个新表
recipe\u components
,其中包含行
recipe\u id | component\u id