Monday, May 22, 2017

Laravel: 'A non-numeric value encountered' when trying to concatenate string

I've created a form and controller, and most of my fields save correctly, however, I have an issue with my 'duration' column.

I have three fields:
mod_start_time
mod_finish_time
mod_duration

And in my code I have a store function with these three lines:

$mod->mod_start_time = Input::get('mod_start_time_hour'.'mod_start_time_minute');
$mod->mod_finish_time = Input::get('mod_finish_time_hour'.'mod_finish_time_minute');
$mod->mod_duration = Input::get('mod_finish_time_hour'.'mod_finish_time_minute' - 'mod_start_time_hour'.'mod_start_time_minute');

The variables that they receive inputs from (in the form) are:
mod_start_time_hour
mod_start_time_minute
mod_finish_time_hour
mod_finish_time_minute

The first two lines of code appear to parse correctly, where the mod_start_time_hour and mod_start_time_minute are concatenated and saved to the database. However, the third line doesn't save correctly, with a non-numeric value being encountered.

I initally thought the problem was that the variables were being saved as a string so I changed the database so that mod_start_time, mod_finish_time and mod_duration are all integers. However, even after change the types of these three variables, the issue still occured.

My question is, am I concatenating it incorrectly, or passing the subtraction operator incorrectly, and if I am, how do I rectifiy this?



via stntmnky

Advertisement