inserting into a database only the filled inputs in a html table

I am building a Laravel 5 application where i am using jQuery DataTables plugin. at the datatable I have 3 input fields in each row. what I want to do is to insert all the row inputs that are filled. In each row you have to fill all 3 inputs so that the data would be posted in database. (The database is sql server.) I have tried something but it’s not working. Here is what I’ve tried in the controller store method:

$dates = $request->get('month');
    $values = $request->get('value');
    $comments = $request->get('comment');
    $reports = []; // this will keep what we input in our DB
    /*if(!empty($row['month']) && !empty($row['value']) && !empty($row['comment'])){
        foreach($row as $rows){
            $index = +1;
        }
    }*/
    if(!empty($dates) && !empty($values)){
    for($i = 0; $i < count($dates); $i++)
    {
        //here i am dividing month and year
        $date = explode('-', $dates[$i]);
        $year = $date[0];
        $month = $date[1]; // <-- line 75

            $reports[] = Reports::create([
                'month' => $month,
                'year' => $year,
                'value' => $values[$i],
                'comment' => $comments[$i]
            ]);

        }
    }
    return $reports;

This throws me an error though:

ErrorException in ReportController.php line 75:
Undefined offset: 1

I’ve searched about it and it says that i’m passing an array instead of a string.

here is the stacktrace (part of it):

at HandleExceptions->handleError('8', 'Undefined offset: 1', 'E:socgensoc-genappHttpControllersReportController.php', '75', array('request' => object(Request), 'dates' => array('2015-06', '2015-02', ''), 'values' => array('369', '22223', ''), 'comments' => array('878', '5466', ''), 'reports' => array(object(Reports)), 'i' => '1', 'date' => array(''), 'year' => '', 'month' => '06')) in ReportController.php line 75

Any help or hint is appreciated


Source: laravel

Leave a Reply