All I want is to have an if statement that exceeds number of absences and show the message.
Here's the SQL query
public function countAbsent()
{
$absent = Attendances::select(DB::raw('count(status)'))
->where('student_id', '=', Auth::user()->id)
->where('status', '=', 'absent')
->where('section_name', 'like', Input::get('section_name'))
->where('subject_code', 'like', Input::get('subject_code'))
->count();
return View::make('student.view_absent')
->with('absent', $absent);
}
You can pass another variable to view:
public function countAbsent()
{
$absent = Attendances::select(DB::raw('count(status)'))
->where('student_id', '=', Auth::user()->id)
->where('status', '=', 'absent')
->where('section_name', 'like', Input::get('section_name'))
->where('subject_code', 'like', Input::get('subject_code'))
->count();
$absent_message = 'Students are not exceeding the absence threshold';
$threshold = 10; //whatever you like
if ($absent > $threshold)
{
$absent_message = 'Students are exceeding the absence threshold';
}
return View::make('student.view_absent')
->with('absent', $absent)
->with('absent_message', $absent_message);
}
And echo the $absent_message in the View student.view_absent.
Related
My laravel "where" is getting ignored it seems. ->where('products.hide_product', '=', 'N')
Any ideas on what I am doing wrong?
$products = Product::join('brands','products.brand_id','=','brands.brand_id')
->join('categories','products.cat_id','=','categories.cat_id')
->leftJoin('images','products.product_id','=','images.product_id')->where('img_priority','=','1')->orWhere('img_priority', '=', null)
->where('products.hide_product', '=', 'N')->where('products.group_id', '=', $groupID)->orderBy('img_priority','DESC')
->get(array('products.en_71','products.astm','images.file_name','products.product_id','products.product_name','products.collect_part_no','brands.brand_name','categories.cat_name','products.status','images.file_type','products.pop','products.color_label','products.ai_complete'));
You can move the join in a closure an use the first where there:
$products = Product::join('brands', 'products.brand_id', '=', 'brands.brand_id')
->join('categories','products.cat_id','=','categories.cat_id')
->leftJoin('images', function ($join) {
$join->on('products.product_id','=','images.product_id')
->where('img_priority','=','1')
->orWhere('img_priority', '=', null);
})
->where(
['products.hide_product', '=', 'N'],
['products.group_id', '=', $groupID],
)
->select('products.en_71', 'products.astm', 'images.file_name', 'products.product_id', 'products.product_name', 'products.collect_part_no', 'brands.brand_name', 'categories.cat_name', 'products.status', 'images.file_type', 'images.img_priority', 'products.pop', 'products.color_label', 'products.ai_complete')
->orderBy('img_priority', 'DESC')
->get();
Please try below code.
$products = Product::join('brands','products.brand_id','=','brands.brand_id')
->join('categories','products.cat_id','=','categories.cat_id')
->leftJoin('images', function($join)) {
$join->on('products.product_id','=','images.product_id')
->where(function($query){
$query->where('img_priority','=','1')
->orWhere('img_priority', '=', null)
})
}
->where(function($query) {
$query->where('products.hide_product', '=', 'N')
->where('products.group_id', '=', $groupID)
})
->orderBy('img_priority','DESC')
->get(array('products.en_71','products.astm','images.file_name',
'products.product_id','products.product_name',
'products.collect_part_no', 'brands.brand_name',
'categories.cat_name', 'products.status',
'images.file_type','products.pop','products.color_label',
'products.ai_complete')
);
I need the same query for two different user roles. Difference is only in one whereNotIn condition.
So for the Basic user it would be:
$chart2 = DB::connection('mysql2')->table('tv')
->select('*')
->join('epgdata_channel', 'cid', '=', 'channelid')
->where('ReferenceDescription', $campaign->spotid)
->whereNotIn('ChannelName', $sky)
->get();
And for Premium:
$chart2 = DB::connection('mysql2')->table('tv')
->select('*')
->join('epgdata_channel', 'cid', '=', 'channelid')
->where('ReferenceDescription', $campaign->spotid)
->get();
I know I can do it with simple if statement:
if($user->userRole == "Basic"){
//first $chart2
}
else{
//second $chart2}
but I have a lots of queries where I need just to add or remove this whereNotin condition and rewriting the queries (using if statement) is not a nice solution.
Try scope.
In your TVModel.php:
public function scopeConditionalWhereNotIn($query, $doesUse, $col, $val) {
if($doesUse)
$query->whereNotIn($col, $val);
}
Usage:
$condi = true;//or false.
$chart2 = TVModel::select('*')
->join('epgdata_channel', 'cid', '=', 'channelid')
->where('ReferenceDescription', $campaign->spotid)
->conditionalWhereNotIn($condi, 'ChannelName', $sky)
->get();
Inside your model add this:
public function scopeBasicUser($query,$channel){
return $query->whereNotIn('ChannelName', $channel);
}
and in your controller:
$query = DB::connection('mysql2')->table('tv')
->select('*')
->join('epgdata_channel', 'cid', '=', 'channelid')
->where('ReferenceDescription', $campaign->spotid);
if($user->userRole == "Basic")
$query = $query->basicUser($channel);
return $query->get();
$userRole = $user->userRole;
$chart2 = DB::connection('mysql2')->table('tv')
->select('*')
->join('epgdata_channel', 'cid', '=', 'channelid')
->where('ReferenceDescription', $campaign->spotid)
->where(function ($query) use ($userRole){
if($userRole == "Basic"){
$query->whereNotIn('ChannelName', $sky)
}
})
->get();
This code worked for me.
Given this code:
$objResellerList - DB::table('ResellerMaster as RM')
->leftJoin('StateMaster as SM','SM.id',RM.id_StateMaster')
->leftJoin('RegionMaster as REM','REM.id','=','RM.id_RegionMaster')
->leftJoin('DealerGroupMaster as DGM','DGM.id','=','RM.id_DealerGroupMaster')
->when($keywords, function($query) use ($keywords) {
return $query->where('RM.company_name', 'like', '%'.$keywords.'%')
->orWhere('DGM.name', 'like', '%'.$keywords.'%')
->orWhere('RM.email', 'like', '%'.$keywords.'%')
->orWhere('RM.pic', 'like', '%'.$keywords.'%')
->orWhere('RM.pic_mobile_no', 'like', '%'.$keywords.'%');
})
->when($status, function($query) use ($status) {
return $query->where('RM.status', $status);
})
->select('RM.*','SM.name as state','REM.name as region', 'DGM.name as dealer_group')
->orderby('RM.company_name','asc')
->paginate(10);
When i search with keywords, I need to close it with a bracket at when condition. How to include the bracket at when condition?
DB::table('users')
->where('name', '=', 'John')
->orWhere(function ($query) {
$query->where('votes', '>', 100)
->where('title', '<>', 'Admin');
})
->get();
give u brackets
select * from users where name = 'John' or (votes > 100 and title <> 'Admin')
so use where with function
->orWhere(function ($query) {
$query->where('votes', '>', 100)
->where('title', '<>', 'Admin');
})
or
->where(function ($query) {
$query->where('votes', '>', 100)
->where('title', '<>', 'Admin');
})
it will give u brackets
use
->when($status,function($query) {
return $query->where(function ($query) {
//do some action
})
})
I have this query:
SELECT * FROM users
WHERE
gender = "$gender" AND country = "$country"
AND ((city = "$city" AND status = "1")
OR (status IN(2,3)))";
How can I write above query in Lumen ?
What I have tried so far is :
$users = User::where('country', '=', $country)
->where('gender', '=', $gender)
->where(function ($users) {
$users->where('city', '=', '$city')
->where('status', '=', "1");
})
->whereIn('status', [2, 3])
->first();
But this query doesn't returns the expected result.
Any idea what is the fault in my query?
Try the following code.
$users = User::where('country', '=', $country)
->where('gender', '=', $gender)
->where(function ($query) use ($city) {
$query->where(function($query) use ($city) {
$query->where('city', '=', '$city')
->where('status', '=', "1");
})->orWhereIn('status', [2, 3]);
})->first();
I'm using laravel 5 in current project. Here is my query to database:
$users = User::where('status', '=', '3')->orWhere('status', '=', '2')->whereExists(function($query1)
{
$query1->select(DB::raw(1))
->from('firsts')
->whereRaw('firsts.user_id = users.id')
->where('status', '=', 'approved');
})
->whereExists(function($query2)
{
$query2->select(DB::raw(1))
->from('seconds')
->whereRaw('seconds.user_id = users.id')
->where('status', '=', 'approved');
})
->whereExists(function($query3)
{
$query3->select(DB::raw(1))
->from('thirds')
->whereRaw('thirds.user_id = users.id')
->where('status', '=', 'approved');
})
->whereExists(function($query4) use($category_id)
{
$query4->select(DB::raw(1))
->from('jobcategories')
->where('provider_id', '!=', 0)
->where('category_id', '=',$category_id);
})
->get();
Idea behind that is to pick all valid users. As you can see at the begining of query I wrote first condition for users. The problem is that users with status = '2' will never have their thirds table status = 'approved'. Is there possibility to put if statment before $query3? Thanks in advance!
Try the query below
$users = User::whereIn('users.status', array(2, 3))
->leftJoin('firsts f', 'f.user_id', '=', 'users.id')
->leftJoin('seconds s', 's.user_id', '=', 'users.id')
->leftJoin('thirds t', 't.user_id', '=', 'users.id')
->where(function($query) {
$query->where('users.status', 2);
$query->where('f.status', 'approved');
$query->where('s.status', 'approved');
})
->orWhere(function($query) {
$query->where('users.status', 3);
$query->where('f.status', 'approved');
$query->where('s.status', 'approved');
$query->where('t.status', 'approved');
});
I'm not sure what to do with the last part of your query, as it has no relation to any of users, firsts, seconds, thirds:
->whereExists(function($query4) use($category_id)
{
$query4->select(DB::raw(1))
->from('jobcategories')
->where('provider_id', '!=', 0)
->where('category_id', '=',$category_id);
})