Query
<?php
$vrec = DB::table('st_sales_live_bk')->where('brandid', $brandid)
->where('branchid', $branchid)
->where('module_id', 1)
->select(DB::raw('trunc(EODATE)'))
->get();
return $vrec;
I was using whereDate() in conditions to truncate a DateTime field. Now I want to select a DateTime field (eodate) using DB::raw, if possible. Any help?
You have to use DB::raw before your where conditions. Your query should look like
$vrec=DB::table('st_sales_live_bk')->select(DB::raw('trunc(EODATE)'))
->where('brandid', $brandid)
->where('branchid', $branchid)
->where('module_id', 1)
->get();
return $vrec;
Related
Are there any way to get difference of two days (today & created_at) using laravel query?
I have tried as follows.but that didn't work for me
public function scopeActivatedOrders($query,$currentDate){
return $query->select('*')
->where('orders.activated', '=', '1')
->where('DATEDIFF(order.arrived_date,$currentDate)','>','14')->get();
}
Thank you!
the problem in your code is that you are using standard where that take a column as the first parameter not an expression ...
you can use whereRaw, or just using DB::raw like:
return $query->select('*')
->where('orders.activated', '=', '1')
->whereRaw('DATEDIFF(order.arrived_date,Now())>14')->get();
note: you can mysql now() function instead of $currentDate variable ...
try this 1st select then add in where condition
public function scopeActivatedOrders($query, $currentDate)
{
return $query->select('orders.*', \DB::raw('DATEDIFF(order.arrived_date, NOW()) as diffday'))
->where('orders.activated', '=', '1')
->get()
->where('diffday', '>', '14');
}
I want to get the template from user_webhook table in my database.In WHERE condition i am checking user_id,app_id and if either notify_admin or notify_customer value is 1 in user_webhook table.I am using query..
$templates= $this->where('notify_admin',1)
->orwhere('notify_customer',1)
->where('user_webhooks.user_id',$user_id)
->where('user_webhooks.app_id',$app_id)
->select( 'webhooks.id as webhook_id','webhooks.app_id','webhooks.topic','webhooks.type','webhooks.action',
'webhooks.sms_template','user_webhooks.id','user_webhooks.notify_admin',
'user_webhooks.notify_customer','user_webhooks.user_id','user_webhooks.sms_template_status',
'user_webhooks.sms_template as sms'
)
->join ('webhooks',function($join){
$join>on('webhooks.id','=','user_webhooks.webhook_id');
})
->get()
->toArray();
when i get query using DB::getQueryLog(), I found the query seems Like
select `telhok_webhooks`.`id` as `webhook_id`, `telhok_webhooks`.`app_id`,
`telhok_webhooks`.`topic`, `telhok_webhooks`.`type`, `telhok_webhooks`.`action`,
`telhok_webhooks`.`sms_template`, `telhok_user_webhooks`.`id`,
`telhok_user_webhooks`.`notify_admin`, `telhok_user_webhooks`.`notify_customer`,
`telhok_user_webhooks`.`user_id`, `telhok_user_webhooks`.`sms_template_status`,
`telhok_user_webhooks`.`sms_template` as `sms` from `telhok_user_webhooks`
inner join
`telhok_webhooks` on `telhok_webhooks`.`id` = `telhok_user_webhooks`.`webhook_id`
where `notify_admin` = ? or `notify_customer` = ? and `telhok_user_webhooks`.`user_id`
= ? and `telhok_user_webhooks`.`app_id` = ?
The result of query giving result of all app_id and user_id.
So Please tell me use of OR in where condition.
Thanks in advance.
You may chain where constraints together as well as add or clauses to the query. The orWhere method accepts the same arguments as the where method:
$users = DB::table('users')
->where('votes', '>', 100)
->orWhere('name', 'John')
->get();
Advanced usage:
Usere::where('id', 46)
->where('id', 2)
->where(function($q) {
$q->where('Cab', 2)
->orWhere('Cab', 4);
})
->get();
The whereIn method verifies that a given column's value is contained within the given array:
$users = DB::table('users')
->whereIn('id', [1, 2, 3])
->get();
More: https://laravel.com/docs/5.5/queries
Change
->where('notify_admin',1)
->orwhere('notify_customer',1)
to
->where(function($q){
$q->where('notify_admin',1)
->orWhere('notify_customer',1);
})
Without this, the orWhere will compare to all other wheres in your query instead of just comparing those two columns
With MySQL, I can use the YEAR() function like this to filter by the year of a date field in a WHERE clause:
SELECT noworkorder FROM workorders WHERE YEAR(date)=2015;
In Laravel, I can of course achieve the same thing with a raw expression:
$data = DB::table('workorders')
->select('noworkorder')
->where(DB::raw('YEAR(date)=2015'))
->orderby('noworkorder', 'desc')
->get();
But is there a way to do this without raw expressions?
The query builder has a whereYear method:
$data = DB::table('workorders')
->select('noworkorder')
->whereYear('date', '=', 2015)
->orderby('noworkorder', 'desc')
->get();
I have next query in Laravel Eloquent:
$buildings = Building::select('buildings.*')->join(
DB::raw('('.
(
IngameBuilding::select('buildings.building_id', 'buildings.level')
->join('buildings', 'buildings.id', '=', 'ingame_buildings.building_id')
->toSql()
).
') as `added_buildings`'), 'added_buildings.building_id', '=', 'buildings.building_id')
->where('buildings.level', '>', 'added_buildings.level')
->get();
This query returns all allowed rows from base, but one row more. When I added DB::raw() in where() return values is valid.
Good-working code:
$buildings = Building::select('buildings.*')->join(
DB::raw('('.
(
IngameBuilding::select('buildings.building_id', 'buildings.level')
->join('buildings', 'buildings.id', '=', 'ingame_buildings.building_id')
->toSql()
).
') as `added_buildings`'), 'added_buildings.building_id', '=', 'buildings.building_id')
->where('buildings.level', '>', DB::raw('`added_buildings`.`level`'))
->get();
Why first code workig, hmm.. Wrong?
I'm not a big fan of Laravel at all.
I've got only small experience with this framework but i'm almost sure that where function accepts only a 'constant' values to be checked against.
If you'll get an output of this query using toSQL method on the query object you will see that eloquent will convert it as something like:
(...) where buildings.level > 'added_buildings.level'
so the condition checks if the buildings.level (whatever the type is)
is greater than the given string and not the column value.
Using the DB::raw you're getting the proper sql as the eloquent won't parse/convert it.
You would need to use whereRaw method I suppose.
http://laravel.com/docs/4.2/queries#introduction
I am trying to retrieve all rows in my table, but with user filters(where conditions).
Table looks like this:
news: id, category, type, body, is_active
I want the user to filter them by: type and is_active
So i am using
if(Input::get("is_active"))
News::where("is_active", 1)->get();
if(Input::get("type"))
News::where("type", Input::get("type"))->get();
if(Input::get("category"))
News::where("category", Input::get("category"))->get();
How can I run all conditions on the same query? I don't want to make if/else for each condition and re-write the query all over again!
This way:
$query = News::newQuery();
if(Input::get("is_active"))
$query->where("is_active", 1)->get();
if(Input::get("type"))
$query->where("type", Input::get("type"))->get();
if(Input::get("category"))
$query->where("category", Input::get("category"))->get();
return $query->get();
You can pass a closure to where()
Try this:
$filters = ['is_avtive', 'type', 'category'];
News::where(function($q) use ($filters){
foreach($filters as $field)
$q->where($field, Input::get($field));
})->get();
Try this
News::where("is_active", 1)
->where('type', Input::get("type"))
->where('category', Input::get("category"))
->get();
or if you want only active news with other or conditions.
News::where("is_active", 1)
->orWhere(function($query)
{
$query->where('type', Input::get("type"))
->where('category', Input::get("category"))
})->get();
Check out advanced where