0

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();
        }

1 Answer 1

1

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'),
]);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Tharaka Dilshan, must I rename my input fields, the type fields are from select input while the date_from and date _to are datetime respectively, if i remove them, my front end UI will not display properly.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.