php - Bind data to DB::raw() in Laravel -



php - Bind data to DB::raw() in Laravel -

i wrote query in raw sql pulls records in given date range, counts records based on day , hr inserted, , calculates hours since "start date" inserted.

i'm attempting stuff query laravel. problem ran needed few things couldn't find in eloquent documentation, had utilize db::raw() accomplish them. worked fine hard-coded values, got point needed utilize dynamic values, led me this question. question worked, problem ran since i'm using clause variables, it's causing weird side effects.

for example, using query:

$clicks = tracker::select(db::raw("time_to_sec(timediff(date_format(actual_date,'%y-%m-%d %h:00:00'),'?'))/60/60+1 hours_since_send"), db::raw('count(*)')) ->where('actual_date', '>', $start_date) ->where('actual_date', '<', $end_date) ->wherein('link_id', $link_ids) ->groupby(db::raw('day(actual_date)')) ->groupby(db::raw('hour(actual_date)')) ->orderby('actual_date', 'asc') ->setbindings([$start_date]) ->get();

gives me error:

sqlstate[hy093]: invalid parameter number (sql: select time_to_sec(timediff(date_format(actual_date,'%y-%m-%d %h:00:00'),'2014-10-02 00:00:00'))/60/60+1 hours_since_send, count(*) `trackers` `actual_date` > ? , `actual_date` < ? , `link_id` in (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) grouping day(actual_date), hour(actual_date) order `actual_date` asc)

which seems straight forwards enough, tried:

$clicks = tracker::select(db::raw("time_to_sec(timediff(date_format(actual_date,'%y-%m-%d %h:00:00'),'?'))/60/60+1 hours_since_send"), db::raw('count(*)')) ->where('actual_date', '>', $start_date) ->where('actual_date', '<', $end_date) ->wherein('link_id', $link_ids) ->groupby(db::raw('day(actual_date)')) ->groupby(db::raw('hour(actual_date)')) ->orderby('actual_date', 'asc') ->setbindings(array_merge([$start_date], $link_ids)) ->get();

and gives me error:

sqlstate[hy093]: invalid parameter number (sql: select time_to_sec(timediff(date_format(actual_date,'%y-%m-%d %h:00:00'),'2014-10-02 00:00:00'))/60/60+1 hours_since_send, count(*) `trackers` `actual_date` > 1156 , `actual_date` < 1157 , `link_id` in (1158, 1159, 1160, 1161, 1162, 1163, 1164, 1165, 1166, 1167, 1168, 1169, 1170, 1171, ?, ?) grouping day(actual_date), hour(actual_date) order `actual_date` asc)

so seems setbindings overriding bindings laravel setting in background. did seek filling them in 1 time again this:

$clicks = tracker::select(db::raw("time_to_sec(timediff(date_format(actual_date,'%y-%m-%d %h:00:00'),'?'))/60/60+1 hours_since_send"), db::raw('count(*)')) ->where('actual_date', '>', $start_date) ->where('actual_date', '<', $end_date) ->wherein('link_id', $link_ids) ->groupby(db::raw('day(actual_date)')) ->groupby(db::raw('hour(actual_date)')) ->orderby('actual_date', 'asc') ->setbindings(array_merge([$start_date, $start_date, $end_date], $link_ids)) ->get();

and although not throw error, returns no data.

so, question: how can utilize db::raw look bound parameter in query without messing ones laravel sets in background? or how can rewrite query accomplish i'm needing?

here original raw query reference:

select time_to_sec(timediff(date_format(actual_date,'%y-%m-%d %h:00:00'),'2014-10-02 00:00:00'))/60/60+1 hours_since_send, count(*) trackers link_id between 1156 , 1171 , actual_date > '2014-10-02 00:00:00' , actual_date < '2014-10-08 00:00:00' grouping day(actual_date), hour(actual_date) order actual_date

don't utilize setbindings, don't need override where bindings (and that's did), this:

$clicks = tracker::select(db::raw("time_to_sec(timediff(date_format(actual_date,'%y-%m-%d %h:00:00'),?))/60/60+1 hours_since_send"), db::raw('count(*)')) ->addbinding($start_date, 'select') ->where('actual_date', '>', $start_date) ...

php mysql laravel-4 eloquent

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -