itsme joe
itsme joe

Reputation: 103

Laravel Save Multiple Data to 1 column

So I have 2 variable for storing the selected time of the user ('time_to' and 'time_from) with these sample data('7:30','8:00') How can I save these two into 1 column('c_time') so it would look like this('7:30-8:00')?

Upvotes: 0

Views: 531

Answers (2)

Mah Es
Mah Es

Reputation: 630

if i understand you correctly, you can create a column of string (varchar) type. and then create the data for your column like this :

$time_to = '7:30';
$time_from= '8:00';
$colValueToBeStored = "(".$time_to."-".$time_from.")";

then just put $colValueToBeStored inside your column.

and to reverse it:

$colValueToBeStored  = "(7:30-8:00)";
$res = explode("-",str_replace([")","("],"",$colValueToBeStored));
$time_to = $res[0];
$time_from = $res[1];

Upvotes: 1

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

define your c_time column type as JSON, that way you can store multiple values, as it will be easier to retrieve as well. Like,

...
$cTime['time_to'] = "7:30";
$cTime['time_from'] = "8:00";
$cTimeJson = json_encode($cTime);
// save to db
...

Upvotes: 1

Related Questions