你好我有一个表的想法,我想返回user_id =与经过身份验证的用户ID的行以及category_id =让我们假设为array [1,2,3]的行。 免责声明:我希望退回这两个条件!
答案 0 :(得分:1)
我知道你想要什么,但
有点问题如果您想要经过身份验证的用户ID和类别ID的结果,您可以
$category_ids = [1, 2, 3];
$model->where('category_id', auth()->user->id)->whereIn('user_id', $ids)->get();
如果您想要经过身份验证的用户或category_ids在数组中的结果,您可以
$category_ids = [1,2,3];
$model->orWhere(function($q) use($category_ids) {
$q->where('category_id', auth()->user->id);
$q->whereIn('user_id', $category_ids);
})->get();
请注意,如果用户未被初始化,这将会中断,但我将其留给您做。
答案 1 :(得分:1)
似乎是这样的:
我认为您的表$categoryIds = [1, 2, 3];
$authUserId = Auth::user()->id;
$ideas = Idea::where('user_id', $authUserId)->whereIn('category_id', $categoryIds)->get();
和与此相关的模型Idea具有下一个结构:
id,user_id,category_id和其他一些字段...
要获得Authenticated用户,您可以执行Auth :: user(); 所以,你可以这样做:
with
如果您需要使用查询提取关系,可以添加$ideas = Idea::with('categories')->where('user_id', $authUserId)->whereIn('category_id', $categoryIds)->get();
语句:
User.php
public function ideas() {
return $this->hasMany('App\Idea');
}
somewhere in code:
$categoryIds = [1, 2, 3];
Auth()->user()->ideas()->whereIn('category_id', $categoryIds)->get();
ofc,您需要在模型中设置实际值
您还可以在用户模型中设置关系并获取与用户关联的记录:
$userIdeas = Idea::where('user_id', Auth::user()->id)->get();
$categoryIds = [1, 2, 3];
$categoryIdeas = Idea::where('user_id', '<>', Auth::user()->id)->whereId('category_id', $categoryIds)->get();
$mergedIdeas = $userIdeas->merge($categoryIdeas);
更新: 要进行某种形式的加入,您可以使用:
$userId = Auth::user()->id;
$categoryIds = [1, 2, 3];
$ideas = Idea::where('user_id', $userId)->orWhere(function($query) use ($categoryIds) {
$query->where('user_id', '<>', $userId)->whereIn('category_id', $categoryIds);
})->get();
最后。要在一个查询中执行此操作:
$firstArr=array("K"=>"location","L"=>"nearaddrss","M"=>"dsdsfll");
$secondArr=array(array("K"=>"loc","L"=>"Aggggkk","M"=>"dsdsfuu","A"=>"jhkhjg","B"=>"nnnn","C"=>"dsmmmmdsf"),array("K"=>"lo","L"=>"Aggggpp","M"=>"dsdsfjj","A"=>"jhkhjg","B"=>"nnnn","C"=>"dsmmmmdsf"));
$firstResultArr=array();
$secondResultArr=array();
$firstResultArr=array();
$secondResultArr=array();
foreach($secondArr as $key2 => $val2) {
foreach($val2 as $key3 => $val3) {
foreach ($firstArr as $key => $value) {
$firstResultArr[$value] = $val2[$key];
}
}
$secondResultArr[] = $firstResultArr;
}
echo json_encode($secondResultArr);exit;