Laravel 5.1 Eloquent: 如何根据某些条件检索集合?
Laravel 5.1 Eloquent: How to retrieve a collection based on some conditions?
我需要根据某些条件筛选项目集合。听说是场景。
//This is part of projects table schema
Schema::create('projects', function (Blueprint $table) {
...
$table->smallInteger('source_lang_id')->index()->unsigned();
$table->smallInteger('target_lang_id')->index()->unsigned();
...
});
//This is part of translator_infos schema
Schema::create('translator_infos', function (Blueprint $table) {
....
$table->smallInteger('from_l_1_id')->index()->unsigned();
$table->smallInteger('from_l_2_id')->index()->nullable()->unsigned();
$table->smallInteger('from_l_3_id')->index()->nullable()->unsigned();
$table->smallInteger('from_l_4_id')->index()->nullable()->unsigned();
$table->smallInteger('to_l_1_id')->index()->unsigned();
$table->smallInteger('to_l_2_id')->index()->nullable()->unsigned();
$table->smallInteger('to_l_3_id')->index()->nullable()->unsigned();
$table->smallInteger('to_l_4_id')->index()->nullable()->unsigned();
....
});
所以每个项目都有源语言和目标语言。翻译人员可能有 4 个语言对。我需要的是过滤项目集合并找到源语言和目标语言至少匹配一种翻译语言对的项目,并将该集合传递给视图。现在我使用的查询如下:
$projects=Project::orderBy('created_at', 'desc')->where('status_id', "=", 1)->paginate(15);
如何将此条件添加到查询中?
我尝试在我的项目模型中使用以下范围,但它只适用于一对语言:
public function scopeLangMatch($query, $from, $to)
{
$match=[
'source_lang_id'=>$from,
'target_lang_id'=>$to
];
return $query->where($match);
}
感谢@Hailwood 的建议,我找到了解决方案。我在项目模型中定义了以下范围:
public function scopeLangMatch($query, $from1,$from2,$from3,$from4, $to1, $to2, $to3, $to4)
{
$match=[
'source_lang_id'=>$from1,
'target_lang_id'=>$to1
];
$match2=[
'source_lang_id'=>$from2,
'target_lang_id'=>$to2
];
$match3=[
'source_lang_id'=>$from3,
'target_lang_id'=>$to3
];
$match4=[
'source_lang_id'=>$from4,
'target_lang_id'=>$to4
];
return $query->where($match)->orWhere($match2)->orWhere($match3)->orWhere($match4);
}
尝试按如下方式重写您的范围:
public function scopeLangMatch($query, $matches) {
$useOr = false;
foreach($matches as $from => $to){
$match=['source_lang_id'=>$from, 'target_lang_id'=>$to];
$query = ($useOr ? $query->orWhere($match) : $query->where($match));
$useOr = true;
}
return $query;
}
然后你就可以把它当作
Project::langMatch([
1 => 2,
3 => 4,
5 => 6,
7 => 8
])->get();
这也使您可以灵活地在将来定义更多或更少的匹配项,而无需修改代码或担心参数匹配。
我需要根据某些条件筛选项目集合。听说是场景。
//This is part of projects table schema
Schema::create('projects', function (Blueprint $table) {
...
$table->smallInteger('source_lang_id')->index()->unsigned();
$table->smallInteger('target_lang_id')->index()->unsigned();
...
});
//This is part of translator_infos schema
Schema::create('translator_infos', function (Blueprint $table) {
....
$table->smallInteger('from_l_1_id')->index()->unsigned();
$table->smallInteger('from_l_2_id')->index()->nullable()->unsigned();
$table->smallInteger('from_l_3_id')->index()->nullable()->unsigned();
$table->smallInteger('from_l_4_id')->index()->nullable()->unsigned();
$table->smallInteger('to_l_1_id')->index()->unsigned();
$table->smallInteger('to_l_2_id')->index()->nullable()->unsigned();
$table->smallInteger('to_l_3_id')->index()->nullable()->unsigned();
$table->smallInteger('to_l_4_id')->index()->nullable()->unsigned();
....
});
所以每个项目都有源语言和目标语言。翻译人员可能有 4 个语言对。我需要的是过滤项目集合并找到源语言和目标语言至少匹配一种翻译语言对的项目,并将该集合传递给视图。现在我使用的查询如下:
$projects=Project::orderBy('created_at', 'desc')->where('status_id', "=", 1)->paginate(15);
如何将此条件添加到查询中? 我尝试在我的项目模型中使用以下范围,但它只适用于一对语言:
public function scopeLangMatch($query, $from, $to)
{
$match=[
'source_lang_id'=>$from,
'target_lang_id'=>$to
];
return $query->where($match);
}
感谢@Hailwood 的建议,我找到了解决方案。我在项目模型中定义了以下范围:
public function scopeLangMatch($query, $from1,$from2,$from3,$from4, $to1, $to2, $to3, $to4)
{
$match=[
'source_lang_id'=>$from1,
'target_lang_id'=>$to1
];
$match2=[
'source_lang_id'=>$from2,
'target_lang_id'=>$to2
];
$match3=[
'source_lang_id'=>$from3,
'target_lang_id'=>$to3
];
$match4=[
'source_lang_id'=>$from4,
'target_lang_id'=>$to4
];
return $query->where($match)->orWhere($match2)->orWhere($match3)->orWhere($match4);
}
尝试按如下方式重写您的范围:
public function scopeLangMatch($query, $matches) {
$useOr = false;
foreach($matches as $from => $to){
$match=['source_lang_id'=>$from, 'target_lang_id'=>$to];
$query = ($useOr ? $query->orWhere($match) : $query->where($match));
$useOr = true;
}
return $query;
}
然后你就可以把它当作
Project::langMatch([
1 => 2,
3 => 4,
5 => 6,
7 => 8
])->get();
这也使您可以灵活地在将来定义更多或更少的匹配项,而无需修改代码或担心参数匹配。