Laravel多条件查询 [英] Laravel multi condition query

查看:71
本文介绍了Laravel多条件查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想搜索用户以前的关系,但不确定如何使这种逻辑发生.

I want to search for users previous relations but not sure how to make this logic happen.

  1. 我的表有2列 from_id to_id (两者都会获取用户ID)
  2. 我正在向后端发送2个ID(一个作为输入,另一个在请求标头中)
  3. 我想说:其中输入ID为from_id,标题ID为to_id OR ,其中输入id为to_id,标题ID为from_id`,返回结果,否则为null显然.
  1. My table has 2 columns from_id to_id (both will get users ids)
  2. I am sending 2 ids to back-end (one as input, another one in request header)
  3. I want to say: whereinput id is from_id and header id is to_idORwhere input id is to_id and header id is from_id` return the result otherwise is null obviously.

注意:在条件 input id为from_id header ID为to_id 的情况下,我所卡住的部分是其中输入id为to_id 标题ID为from_id

Note: The part that I'm stuck in is and in conditions input id is from_id and header id is to_id OR where input id is to_id and header id is from_id

目前,我有一半的代码是这样的:

currently I have half way code which is this:

public function check(Request $request) {
  // user id from header request (request sender) e.g `1`
  $user = $request->user();
  // second user id (not sure where to use it yet) e.g `4`
  $request->input('receiverId');

  $chat = PrivateChat::where('from_id', $user->id)->orWhere('to_id', $user->id)->first();
}

简单起见

基本上,无论谁是发送者(from_id)和谁是接收者(to_id),我都希望返回2个ID之间的旧聊天记录.

Basically I want return old chat between the 2 ids regardless of who was sender (from_id) and who was receiver (to_id).

有什么主意如何实现我的逻辑吗?

Any idea how to make my logic happen?

这是我的完整代码,当我确实在ID为 1 4 的用户之间进行先前的聊天时,它始终返回false.

this is my full code and it always return false while i do have previous chat between users with id 1 and 4.

public function check(Request $request) {
    $user = $request->user();

    $chat = PrivateChat::where([['from_id', $user->id],['to_id',$request->input('receiverId')]])
    ->orWhere([['from_id', $request->input('receiverId')],['to_id',$user->id]])->first();

    $receiver = User::where('id', $request->input('receiverId'))->first();

    if($chat) {
        return response()->json([
            'data' => new PrivateChatResource($chat),
            'exist' => true,
            'receiver' => new UserResource($receiver),
            'message' => 'Chat data retrieved successfully.'
        ]);
    } else {
        return response()->json([
            'exist' => false,
            'receiver' => new UserResource($receiver),
            'message' => 'Chat data retrieved successfully.'
        ]);
    }
}

推荐答案

我看到您想执行布尔逻辑,如果出错,请纠正我.

As i see it you want to do the boolean logic, if wrong please correct me.

(from_id = header_id AND to_id = header_id) OR (from_id = input_id AND to_id = input_id)

这可以通过以下 Eloquent 查询获得.使用 where($ cloure)语法,执行上面列出的布尔逻辑.用闭包调用where,在逻辑中加上括号,clojure中的所有内容都将包裹在()中.

This can be obtained with the following Eloquent query. Using where($cloure) syntax, for doing the boolean logic listed above. Calling where with a closure, adds parenthesis to your logic, everything within the clojure will be wrapped in ().

$user = $request->user();

$userInput = $request->input('receiverId');

PrivateChat::where(function ($query) use ($user) {
    $query->where('from_id', $user->id)->orWhere('to_id', $user->id);
})->orWhere(function ($query) use ($userInput) {
    $query->where('from_id', $userInput)->orWhere('to_id', $userInput);
})->first();

这篇关于Laravel多条件查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆