Francis
Francis

Reputation: 53

Storing Array Data in database with Laravel

My form data is returning this json file which i have to store as multiples rows in database.

JSON

    {
     "name": john,
     "department" : "IT"
     "type" : ['annual', 'private'],
     "from_date" : ['2020-09-09', '2020-10-08'],
     "to_date" : ['2020-09-12', '2020-10-15'],
     "hours" : ['06','09'],
   }

I want to store this in the database using laravel and the output should be

  id      name    department   type     from_date     to_date       hours
  1       John       IT         annual   2020-09-09   2020-10-08      06
  2       John       IT         Private  2020-09-12   2020-10-15      09

Here is my laravel code, I tried this but not working fine, Please i need assistance, I am new to laravel. Thanks

   function getLeave(Request $request){
         $leaveInfo = new LeaveTable();
         $leaveInfo->name = $request->name;
         $leaveInfo->department = $request->department;
         if(count($leaveInfo->type >= 1)){
          for( $i = 0; $i < count($leaveInfo->type); $i++){
           $leaveInfo->type = $request->type[$i];
          }
         }
         if(count($leaveInfo->date_from >= 1)){
             for( $i = 0; $i < count($leaveInfo->date_from); $i++)
                   $leaveInfo->date_from = $request->date_from[$i];
                   }
              }
        if(count($leaveInfo->date_to >= 1)){
             for( $i = 0; $i < count($leaveInfo->date_to); $i++)
                   $leaveInfo->date_to = $request->date_to[$i];
                   }
              }
         if(count($leaveInfo->hours >= 1)){
            for( $i = 0; $i < count($leaveInfo->hours); $i++){
               $leaveInfo->hours = $request->hours[$i];
             }
         }
                         
           $leaveInfo->save();
        }

Upvotes: 0

Views: 101

Answers (1)

Tharaka Dilshan
Tharaka Dilshan

Reputation: 4499

To achieve this you need to create text columns for 'type', 'from_date', 'to_date', 'hours' columns.

In the migration

$table->text('type');
$table->text('from_date');
$table->text('to_date');
$table->text('hours');

Then in the modal, you need to add these columns to the casts array.

In the modal

protected $casts = [
    'type' => 'array',
    'from_date' => 'array',
    'to_date' => 'array',
    'hours' => 'array',
];

Then in the controller, It is easy.

In the controller

$leaveInfo = new LeaveTable();

$leaveInfo->name = request->name;
$leaveInfo->department = request->department;
$leaveInfo->type = request->type;
$leaveInfo->from_date = request->from_date;
$leaveInfo->to_date => request->to_date;
$leaveInfo->hours => request->hours;

$leaveInfo->save();

Side Note

  • Rename your modal name from 'LeaveTable' to 'Leave' or 'LeaveInfo'
  • Rename those columns to plural names since they contains many data. ex: 'types'
  • There is an alternative way to write this controller code.
LeaveTable::create([
    'name' => request('name'),
    'department' => request('department'),
    'type' => request('type'),
    'from_date' => request('from_date'),
    'to_date' => request('to_date'),
    'hours' => request('hours'),
]);

Upvotes: 1

Related Questions