Laravel-5.6'LIKE'在何处或何处选择

时间:2019-02-27 09:56:10

标签: php mysql laravel laravel-5 laravel-5.6

我使用此查询,但出现错误:

$description = $request->get('description');
                $description_query = Transcationhistorique::where(function ($query) use ($description, $user_id) {
                    $query->where('receiver_id', $user_id,'description', 'LIKE','%' . $description . '%')
                        ->orWhere('description', 'LIKE','%' . $description . '%','sender_id', $user_id);
                })->get();

这是我得到的错误:

  

“ SQLSTATE [42S22]:找不到列:1054'where中的未知列'0'   子句”(SQL:从transcation_historique中选择*,其中   (sender_id = 32,0 =%salaire%,1 =相似,2 =   说明)或receiver_id = 32)“

这就是我真正想要运行的:

select * from `transcation_historique` where (`sender_id` = 32 and `description` = %salaire%) or (`receiver_id` = 32 and `description` = %salaire%)

4 个答案:

答案 0 :(得分:5)

尝试

Transcationhistorique::where(function ($query) use ($description, $user_id) {
            $query->where(['receiver_id' => $user_id, 'description' => 'LIKE %' . $description . '%'])
                ->orWhere(['description' => 'LIKE %' . $description . '%', 'sender_id' => $user_id]);
        })->get();

答案 1 :(得分:1)

然后,您应使用以下语句:

$query = Transcationhistorique::where(function ($query) use ($description, $user_id) {
      $query->where('receiver_id', $user_id)
          ->where('description', 'LIKE','%' . $description . '%');
})
->orWhere(function ($query) use ($description, $user_id) {
      $query->where('sender_id', $user_id)
          ->where('description', 'LIKE','%' . $description . '%');
})
->get();

答案 2 :(得分:0)

您应该尝试以下操作:

$description = $request->get('description');
$query = Transcationhistorique::where(function ($query) use ($description, $user_id) {
      $query->where('receiver_id', $user_id)
          ->where('description', 'LIKE','%' . $description . '%');
})
->orWhere(function ($query) use ($description, $user_id) {
      $query->where('sender_id', $user_id)
          ->where('description', 'LIKE','%' . $description . '%');
})
->get();

答案 3 :(得分:0)

或尝试使用Query Builder:

$description_query = DB::table('transcationhistoriques')
            ->where(['receiver_id' => $user_id, 'description' => 'LIKE %' . $description . '%'])
            ->orWhere(['description' => 'LIKE %' . $description . '%', 'sender_id' => $user_id])
            ->get();