选择数据并在哪里和哪里使用laravel 5.6

时间:2019-02-26 09:09:37

标签: php laravel laravel-5 laravel-5.6

我试图创建((Where and Where)OR(Where and Where)),经过大量搜索,我发现了这个

DbContext

我得到一个空结果

  

“最近3笔交易”:[]

4 个答案:

答案 0 :(得分:6)

如果您要搜索简单的where or where,请使用(使用查询):

$last_transations = Transcationhistorique::where('sender_id', $user_id)
                    ->orWhere('receiver_id', $user_id)
                    ->orderBy('created_at', 'desc')->skip(3)->take(3)->get();

如果您要搜索where and where,请使用(使用查询):

$last_transations = Transcationhistorique::where(['sender_id' => $user_id, 'receiver_id' => $user_id])
                    ->orderBy('created_at', 'desc')->skip(3)->take(3)->get();

如果您要搜索(where and where) OR (where and where),那就有点复杂了,您将进行如下查询:

DB::table('users')
   ->where(['col1' => 'val1', 'col2' => 'val2'])
   ->orWhere(function ($query) {
         $query->where(['col3' => 'val3', 'col4' => 'val4']);
   })
   ->get();

引用here

答案 1 :(得分:1)

我认为您指定的跳过和取值错误,可以尝试以下操作:

$last_transations = Transcationhistorique::where('sender_id', $user_id)
                    ->orWhere('receiver_id', $user_id)
                    ->orderBy('created_at', 'desc')->skip(0)->take(3)->get();

在文档中显示:

  

跳过/获取:限制查询返回的结果数,或跳过   在查询中给出给定数量的结果时,您可以使用跳过并接受   方法:

您正在做的是跳过3并取3,所以就像->offset(3)->limit(3),它将返回空!

答案 2 :(得分:0)

您可以使用where并将一个函数传递给它,例如:

如果我想获取具有管理员ID 4或5的用户

$users->where("type","admin")->where(function($query){
        $query->where("id",4)->orWhere("id",5);
    })->get();

等效于:

SELECT * FROM users WHERE type = "admin" AND (id = 4 OR id = 5)

答案 3 :(得分:0)

  $last_transations = Transcationhistorique::where([['sender_id', '=', $user_id],['receiver_id','=', $user_id]])
                            ->orderBy('created_at', 'desc')
                            ->skip(3)
                            ->take(3)
                            ->get();

你去了。