Laravel between clause returning null - mysql

I get a SQL in laravel like this
$scheduleMap=array("schedule_id"=>$scheduleId);
$scheduleConsume=DB::connection("mysql_report")->table("xxx")
->where($scheduleMap)
->whereBetween(DB::Raw("TO_DAYS(FROM_UNIXTIME(report_time))"),array(DB::Raw("TO_DAYS('".$startDate."')"),DB::Raw("TO_DAYS('".$startDate."')")))
->select(DB::Raw("SUM(imp_num) as imp_num"))
->first();
and I printed SQL as
select SUM(imp_num) as imp_num from `xxx`
where TO_DAYS(FROM_UNIXTIME(report_time))
between TO_DAYS('2016-11-05')
and TO_DAYS('2016-11-12')
Then I try to run sql in navicat,it works well,but in laravel,it return null.
I can't understand! I have to use whereRaw now.

Try this :)
$scheduleConsume = DB::connection("mysql_report")
->table('xxx')
->where('schedule_id', $scheduleId)
->whereBetween(
DB::Raw("TO_DAYS(FROM_UNIXTIME(report_time))"), [
DB::Raw("TO_DAYS('".$startDate."')"),DB::Raw("TO_DAYS('".$startDate."')")
]
)
->select(DB::Raw("SUM(imp_num) as imp_num"))
->first();

Related

Rewrite MYSQL query using Laravel query builder

I'm trying to rewrite MYSQL query using Laravel query builder
So this is the query
DB::select("SELECT * FROM accomodation WHERE id NOT IN
(SELECT accomodation_id FROM bookings
WHERE (`checkin` <= '$request->in' AND `checkout` >= '$request->out'));");
And this is what I've done so far
Accommodation::whereNotIN('id', function($q)
{
$q->select('accomodation_id')
->from('bookings')
->where('checkin', '<=', '$request->out')
->where ('checkout', '>=', '$request->in');
})
->get();
The problem is the first query is giving me expected results but the second one isn't
Can someone point out what I'm doing wrong?
I think you type it wrong. Look at the $request->in and $request->out. You may want to flip it. and why you use '$request->in' instead of $request->in. It will parse as string instead of the value
You don't have use. For example php Accommodation::whereNotIN('id', function($q) use ($request) and you need to flip condition with request ;)

Laravel query join on not null

I have the following query join in MySQL.
left join the_fields as tf on
tf.tf_kode = x.tf_kode
and tf.is_payed is not null
I tried to convert to laravel query builder like this:
->leftJoin('the_fields as tf', function ($j) {
$j->on('tf.tf_kode', '=', 'x.tf_kode');
$j->on('tf.is_payed','!=', null);
})
But it shows the error unknown column '' on clouse.
Please help. Thanks!
->on method arguments are columns and operators, You cant pass where condition in it.
You can use this:
->leftJoin('the_fields as tf', function ($j) {
$j->on('tf.tf_kode', '=', 'x.tf_kode')->whereNotNull('tf.is_payed');
});
Hope this helps you

Laravel wherebetween with orwherebetween

I have written a Laravel query. I am using whereBetween and orWhereBetween along with a where statement on a column named 'shareable', which should return one row.
But this query does not take where condition into consideration. It took only whereBetween and orWhereBetween. What can be the reason.
my query
$childs = Program::whereIn('id', $programs_by_date)
->where('shareable', '=','1')
->wherebetween('starting_date', [$new_start_date,$new_end_date])
->orwherebetween('ending_date', [$new_start_date,$new_end_date])
->orderBy('starting_date')
->get();
->where('shareable', '=','1') return 0 rows. What can be the reason.
shareable type is enum
You should wrap whereBetween & orWhereBetween in where. Try this:
$childs = Program::whereIn('id', $programs_by_date)
->where('shareable', '=','1')
->where(function($query) use ($new_start_date, $new_end_date){
$query->whereBetween('starting_date', [$new_start_date,$new_end_date])
->orWhereBetween('ending_date', [$new_start_date,$new_end_date]);
})
->orderBy('starting_date')
->get();
Hope this will work.
You Should try this
$child = Program::whereIn('id', $programs_by_date)
->where('shareable', '=','1')
->whereRaw("( starting_date BETWEEN '".$new_start_date."' AND '".$new_end_date."' OR ending_date BETWEEN '".$new_start_date."' AND '".$new_end_date."' ")
->orderBy('starting_date')
->get();

Use `AND` in Laravel Query Builder, syntax error (orOn)

I have this sql (MariaDB) query, it works ok:
SELECT
admin_combustibles.nombre,
admin_combustibles.id_combustible,
admin_combustible_unidades.nombre AS unidades,
admin_tipo_combustibles.nombre AS tipo_combustible,
admin_indice_combustibles.valor_combustible
FROM
admin_combustibles
INNER JOIN admin_combustible_unidades ON admin_combustibles.combustible_unidades_id = admin_combustible_unidades.id_combustible_unidad
INNER JOIN admin_tipo_combustibles ON admin_combustibles.tipo_combustible_id = admin_tipo_combustibles.id_tipo_combustible
LEFT JOIN admin_indice_combustibles ON admin_combustibles.id_combustible = admin_indice_combustibles.combustible_id
AND admin_indice_combustibles.anio = 1992
AND admin_indice_combustibles.mes = 6
ORDER BY
admin_combustibles.nombre
But when I try to make it through Laravel, I have some error because Laravel put some on the number 1992, in that way the query doesn't work.
Plus, I don't know how to do the AND on Laravel, I only have orOn but it doesn't work:
$datos = AdminCombustible::select([
'admin_combustibles.nombre',
'admin_combustibles.id_combustible',
'admin_combustible_unidades.nombre AS unidades',
'admin_tipo_combustibles.nombre AS tipo_combustible',
'admin_indice_combustibles.valor_combustible'])
->join('admin_combustible_unidades', 'admin_combustibles.combustible_unidades_id', '=', 'admin_combustible_unidades.id_combustible_unidad')
->join('admin_tipo_combustibles', 'admin_combustibles.tipo_combustible_id', '=', 'admin_tipo_combustibles.id_tipo_combustible')
->leftJoin('admin_indice_combustibles', function ($join) {
$join->on('admin_indice_combustibles.combustible_id', '=', 'admin_combustibles.id_combustible')->orOn('admin_indice_combustibles.anio', '=', 1992);
})
->get();
dd($datos);
I got an sql syntax error.
Any help on how to make that query with query builder of Laravel? (I put the entire query as DB::raw and it works, but I don't want to use raw() )
I think there might be two problems here. Since you are using and in your original query, you might be needing to use or instead of orOn on your join.
Also you will need to use DB::raw() on the parameters, otherwise Laravel will consider them columns....
$join->on('admin_indice_combustibles.combustible_id', '=', 'admin_combustibles.id_combustible')->on('admin_indice_combustibles.anio', '=', DB::raw('1992'));

why codeigniter active record 'LIKE' not work in update query?

$array=array(
'amount'=>$this->input->post('netamt'),
'trandate'=>$billdate,
'crcode'=>$this->input->post('rcode')
);
$this->db->where('voucher',$this->input->post('billno'));
$this->db->like('code','16','after');
$this->db->update('tranachst',$array);
when display this query using
echo $this->db->last_query();
UPDATE `tranachst` SET `amount` = '717360', `trandate` = '2015-07-15', `crcode` = '311001' WHERE `voucher` = '15020'
here like query not working , why ?
Please Try This --
My Exmaple --
$this->db->where('faq_id', $id);
$this->db->where('question LIKE ', '%do%');
$this->db->update('admin_faqs', $data);
Your Example --
$array=array(
'amount'=>$this->input->post('netamt'),
'trandate'=>$billdate,
'crcode'=>$this->input->post('rcode')
);
$this->db->where('voucher',$this->input->post('billno'));
$this->db->where('code LIKE ', '16%');
$this->db->update('tranachst',$array);
In this code you don`t need to use after, before parameter please try this..
Some people have the same question, and until now i don't know why. but clearly CI didn't support active record like above.
you can use $this->db->query() or
function index(){
$this->load->database();
$data=array(
'userlogin'=>'test'
);
$this->db->where('userlogin','xx');
$this->db->where("email like 'xx'");
$q=$this->db->update('master_user',$data);
}
something like that work too.
the same question : CodeIgniter Active Record 'like' ignoring 'where'