Laravel: 如何在一个查询中写两个查询并从数据库中获取结果?
Laravel: How to write two query in one query and get result from database?
我有获取当前用户所有已删除邮件的示例代码:
$query1 = $inbox->where('recipient', $user_id)
->where('trashed_in_recipient', 1)
->where('show_in_recipient', '!=', 1)
->orderBy('created_at', 'desc')
->paginate($paginate);
$query2 = $inbox->where('sender', $user_id)
->where('trashed_in_sender', 1)
->where('show_in_sender', '!=', 1)
->orderBy('created_at', 'desc')
->paginate($paginate);
如何将两个查询合二为一并得到最终结果?
Table结构:
您可以使用函数来定义 WHERE
个组:
$query = $inbox->where(function($query) use ($user_id) {
$query->where('recipient', $user_id);
$query->where('trashed_in_recipient', 1);
$query->where('show_in_recipient', '!=', 1);
})->orWhere(function($query) use ($user_id) {
$query->where('sender', $user_id);
$query->where('trashed_in_sender', 1);
$query->where('show_in_sender', '!=', 1);
})->orderBy('created_at', 'desc')
->paginate($paginate);
希望对您有所帮助
我有获取当前用户所有已删除邮件的示例代码:
$query1 = $inbox->where('recipient', $user_id)
->where('trashed_in_recipient', 1)
->where('show_in_recipient', '!=', 1)
->orderBy('created_at', 'desc')
->paginate($paginate);
$query2 = $inbox->where('sender', $user_id)
->where('trashed_in_sender', 1)
->where('show_in_sender', '!=', 1)
->orderBy('created_at', 'desc')
->paginate($paginate);
如何将两个查询合二为一并得到最终结果?
Table结构:
您可以使用函数来定义 WHERE
个组:
$query = $inbox->where(function($query) use ($user_id) {
$query->where('recipient', $user_id);
$query->where('trashed_in_recipient', 1);
$query->where('show_in_recipient', '!=', 1);
})->orWhere(function($query) use ($user_id) {
$query->where('sender', $user_id);
$query->where('trashed_in_sender', 1);
$query->where('show_in_sender', '!=', 1);
})->orderBy('created_at', 'desc')
->paginate($paginate);
希望对您有所帮助