3
我有一個大的搜索查詢和所有的偉大工程,但我需要給每個用戶的軟件包(研討會)顯示該刀片搜索查詢Laravel輸入
<tbody>
@foreach($users as $i=>$user)
<tr>
<td>{{ $i+1 }}</td>
<td>{{ $user->home_lastname }}</td>
<td>{{ $user->home_firstname }} </td>
//------> <td> HERE I NEED TO SHOW ALL THE PACKAGES NAMES(SEMINARS) FOR EACH USER</td>
<td>{{ $user->home_id }}</td>
<td></td>
UserController的
$builder = User::query();
if (Input::has('lastname')) {
$queryString = Input::get('lastname');
$builder->where('home_lastname', 'LIKE', "%$queryString%");
}
if (Input::has('name')) {
$queryString = Input::get('name');
$builder->where('home_firstname', 'LIKE', "%$queryString%");
}
if (Input::has('id')) {
$queryString = Input::get('id');
$builder->where('home_id', 'LIKE', "%$queryString%");
}
if (Input::has('type-select')){
$userType = Input::get('type-select');
$builder->select('*')
->where('Home_Students.home_elv_rights',$userType)
->get();
}
if(Input::has('seminar-select')){
$seminar = Input::get('seminar-select');
$builder->select('*')
->join('Home_StudentPackages','Home_StudentPackages.hsp_homeStudID','=','Home_Students.home_id')
->join('Home_Packages','Home_Packages.Package_ID','=','Home_StudentPackages.hsp_packid')
->where('Home_Packages.Package_ID',$seminar)
->get();
}
if(Input::has('class-select')){
$class = Input::get('class-select');
$builder->select('*')
->join('Hw_StudentClasses','Hw_StudentClasses.Stclass_studentid','=','Home_Students.home_id')
->join('Hw_Classes','Hw_Classes.Class_id','=','Hw_StudentClasses.Stclass_classid')
->where('Hw_Classes.Class_id',$class)
->get();
}
if(Input::has('date_created_from')) {
$date = date("Y-m-d", strtotime(Input::get('date_created_from')));
$builder->select('*')
->whereBetween('Home_Students.home_dateCreated', [$date, date("Y-m-d")])
->get();
}
elseif(Input::has('date_created_from','date_created_untill')){
$date = date("Y-m-d", strtotime(Input::get('date_created_from')));
$date2 = date("Y-m-d", strtotime(Input::get('date_created_untill')));
$builder->select('*')
->whereBetween('Home_Students.home_dateCreated', [$date,$date2])
->get();
}
elseif(Input::has('date_created_untill')){
$date2 = date("Y-m-d", strtotime(Input::get('date_created_untill')));
$date = date("Y-m-d", strtotime(Input::get('date_created_from')));
$builder->select('*')
->whereBetween('Home_Students.home_dateCreated',[$date,$date2])
->get();
}
// ... more clauses from the querystring
$users = $builder->orderBy('home_lastname')->get();
return View::make('user.show')->with(array('users' => $users));
}
我試過這些連接到開始$構建器查詢和控制器結束的生成器,但它沒有工作。如果沒有工作,每個wheres也沒有工作,所以我不知道任何其他方式來做到這一點。
DB::table('Home_Students')
->select('home_lastname','home_firstname','home_id','Package_Name')
->join('Home_StudentPackages','Home_StudentPackages.hsp_homeStudID','=','Home_Students.home_id')
->join('Home_Packages','Home_Packages.Package_ID','=','Home_StudentPackages.hsp_PackID'
->get();
我只需要要爲每個用戶Packages_Names我的刀片看法,但有一個問題與研討會(包)的搜索。
編輯
我試圖在用戶的模型來創建公共職能,我會得到每個用戶的所有包(研討會);你知道有什麼辦法做到這一點?
有一個數據透視表,結合了用戶和軟件包!
編輯2我認爲這是錯誤的,因爲它返回[{「Package_Name」:「Excel」}] - 是否有可能返回這個字符串?
public function getUserSeminars(){
$users = Seminar::select('Package_Name')
->join('Home_StudentPackages','Home_StudentPackages.hsp_PackID','=','Home_Packages.Package_ID')
->join('Home_Students','Home_Students.home_id','=','Home_StudentPackages.hsp_homeStudID')
->where('Home_Students.home_id','=',$this->home_id)
->get();
return $users;
}
意識到您正在使用此方法運行n + 1個查詢。 – veksen
我不能用另一種方法做,因爲我有一個如果這樣做相同的查詢,所以它以任何其他方式割裂,另一種方式,我嘗試了它給我的結果,但它給了我3或4次用戶爲每個研討會,我不能做除此之外還有其他的建議嗎? –