首页 > 解决方案 > 如何在 laravel 中添加“和”条件?

问题描述

我想添加另一个条件(L_Examen_Objectif::where('libelle', '=', $request->input('libelle') )->exists())

条件如果我得到相同的libelle具有相同的id它应该返回True(我不想插入具有相同 id 的相同的 libelle。)

这是我的代码:

 public function addPost(Request $request, $id)
 {
     if (L_Examen_Objectif::where('libelle', '=', $request->input('libelle') )->exists()) {
         return  'true';
     }
     else {
         $Examen_data = array(
             'libelle' => $request->input('libelle'),
             'id_examen' => $request->input('id_examen'),
             'id_lexamen' => $id,
         );
         L_Examen_Objectif::insert($Examen_data);    
     }
 }

标签: phplaravel

解决方案


您可以通过多种方式做到这一点。一种解决方案是:

L_Examen_Objectif::where([
   ['libelle', '=', $request->input('libelle')],
   ['other_key', '=', $request->input('libelle')],
  ])

这是这种方式的模式:

$query->where([
    ['column_1', '=', 'value_1'],
    ['column_2', '<>', 'value_2'],
    [COLUMN, OPERATOR, VALUE],
    ...
])

您可以在此处找到更多信息:如何使用 Laravel Eloquent 创建多个 Where 子句查询?


推荐阅读