如何从用户文本输入中搜索匹配的数据库记录并避免提取信息((菲律宾)

57hvy0tb  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(204)

我有两个数据库表,配方表和配料表。一个配方可以包含许多成分,它们之间的关系由配方id链接。
配方表
身份证件
适合
ReceiveCreditsMain表
身份证件
配方id
IngCreditName
我有一个用户输入表单,用户在其中决定向他们推荐哪些食谱的约束条件。我有一个文本框输入,允许用户输入“特定过敏”。然后,该值将在配料表中搜索具有名称的匹配项,如果通过查询关系缺失找到匹配项,则避免提取配方id。
例如,如果用户输入“菠萝”,如果在配料表中找到匹配项,则可以避免提取配方id。
配方在模型中有许多关系:

public function ingredients()
{
return $this->hasMany(Ingredients::class, 'recipe_id');
}

拉动用户输入:

$suited = $request->suitable_for;
$specificallerg = $request->specific_allergen;

控制器中的代码:

$recipenew = Recipe::where('recipe.suitable_for', $suited)
    ->whereDoesntHave('ingredients', function($QBspecificallergens) use($specificallerg) {
        $QBspecificallergens->where('recipeingredientsmain.ingredientname', 'like', '%specificallerg%');
      })->pluck('id');

代码正在运行,但我的问题是一个逻辑错误,因为它正在“ingredientname”中搜索字面上的“%specificallerg%”,而不是输入的值。我怎样才能解决这个问题?

alen0pnh

alen0pnh1#

您需要从前端获取所请求的字符串。不是你从表中搜索的那个。

$suited = $request->suitable_for;
$specificallerg = $request->specific_allergen;

$recipenew = Recipe::where('recipe.suitable_for', $suited)
    ->whereDoesntHave('ingredients', function($QBspecificallergens) use($specificallerg) {
        $QBspecificallergens->where('recipeingredientsmain.ingredientname', 'like', '%'.$specificallerg.'%');
      })->pluck('id');

相关问题