我想为所有应用程序查询添加一个过滤器,以获得特定年份的结果.
我将当前年度纳入会议,如下所示:
public function postLogin()
{
Session::put('currentYear', date("Y"));
}
Run Code Online (Sandbox Code Playgroud)
我的应用程序中有很多控制器.我希望任何模型的查询结果按会话年过滤=> Session :: get('currentYear')
我有很多模特; 例如,我有一条路线可以查看所有用户,教师和学生.
public function getList()
{
$data['students'] = User::where('group_id', '=', 4)->get();
return View::make('students.list', $data);
}
Run Code Online (Sandbox Code Playgroud)
我可以放入__construct
BaseController过滤所有应用程序查询Session::get('currentYear')
吗?
我正在使用Laravel 4.2构建应用程序。我有一个模型units
和另一个users
表和数据透视表user_units
。该应用程序中的每个用户都可以选择一个单元并将其添加到他的收藏夹列表中,然后他可以将其信息发布为该单元。
我要选择所有用户发布的所有单元
的user_units
(枢转)表具有以下的列:
id
user_id
unit_id
publish
adtype
addinfo
created_at
updated_at
Run Code Online (Sandbox Code Playgroud)
在模型上使用关系方法
public function users() {
return $this->belongsToMany('User', 'user_units')
->withPivot('id','publish', 'adtype', 'addinfo');
}
public function units() {
return $this->belongsToMany('Unit', 'user_units')
->withPivot('id','publish', 'adtype', 'addinfo');
}
Run Code Online (Sandbox Code Playgroud)
我的查询以选择所有用户的所有已发布单位
// Get all published units by users for sale.
$users = User::all();
$publishedSaleUnits = [];
foreach($users as $user){
$userUnits = $user->units()->orderBy('adtype', 'desc')->get();
if(count($userUnits)){
foreach($userUnits as $unit){
if($unit->pivot->publish == 1 && $unit->unit_purpose_id == 1){
if( $unit->pivot->adtype ){ …
Run Code Online (Sandbox Code Playgroud) 我需要用Eloquent选择当年创建的记录
到目前为止,这是我正在使用的代码.如何过滤结果以仅检索当前年份中创建的结果?
public function vacationsbalance($typeId) {
// Get vacataions balance for existing employee.
$vacationsBalance = $this->vacations()->where('vacation_type_id', '=', $typeId)->sum('days_num');
return $vacationsBalance;
}
Run Code Online (Sandbox Code Playgroud)