我需要根據一些條件篩選項目集合。聽到是這種情況。Laravel 5.1雄辯:如何根據某些條件檢索集合?
//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);
}
您將需要多個建議設置範圍(http://laravel.com/docs/5.1/eloquent#query-scopes),以便您可以傳遞調用'Project :: langMatch($源,$ to) - > orderBy(...)...'。 – Hailwood
@Hailwood使用這種方法,我只能過濾一對語言。我已經將這個範圍添加到了我的問題中。還有其他解決方案嗎? –