ejdickerson
ejdickerson

Reputation: 95

Laravel: upload multiple rows created from arrays

I'm building an application with Laravel which communicates with an OpenCPU server to perform some calculations. The OpenCPU server returns data in JSON format, which I then process to pull out the relevant information. This data returns a sku, retailer, date and sales. These are then posted to a controller using AJAX. Within this controller I then want to upload this data into the database by creating a new array of data to upload in one go. Each row should have a sku, retailer, date and sales. The date field in the database is called date, but called obs in the code.

OpenCPU returns JSON which is the parsed to a Javascript object using

var data = JSON.parse(output);

After logging to the Javascript console I get an array of the correct length, with the sales numbers.

The data is then sent to a Laravel controller via AJAX

$('#save').click(function(){
    var id = $('#filter option:selected').text();
        var json = $.ajax({
            url: 'sales/' + id + '/update',
            type: 'POST',
            data:  {
                    'sku': $('#sku').text(),
                    'retailer': $('#retailer').text(),
                    'obs': data.OBS,
                    'sales': data.Sales,
                    },
            async: false
        }).responseText;
    var message = JSON.parse(json);
    $('.flash').html(message).fadeIn(300).delay(2500).fadeOut(300);
});

In Laravel I then try to store the data in a MySQL database using the following

$sku = Input::get('sku');
$retailer = Input::get('retailer');
$obs = Input::get('obs');
$sales = Input::get('sales');

foreach($obs as $key => $n ){
    $arrayData[] = array(
        'sku' => $sku,
        'retailer' => $retailer,
        'date' => $obs[$key]
        'sales' => $sales[$key]
    );
}
Chart::create($arrayData);

However the above code doesn't appear to work. The following code will create the correct number of rows in the database with the sku and retailer populated, but the sales figure is just the loop number, rather than the number of sales

$sku = Input::get('sku');
$retailer = Input::get('retailer');
$dates = Input::get('obs');
$sales= Input::get('sales');

foreach(range(1, count($dates)) as $key){
    DB::table('charts')->insert(
        [
            'sku' => $sku,
            'retailer' => $retailer,
            'date' => DateTime($obs[$key]),
            'sales' => $sales[$key]
        ]
    );
}

Given that the sku and retailer are a single input and repeated, I expect it's either an issue with passing the array to Laravel or the way in which I'm trying to access the elements in the 'obs' and 'sales' array

Upvotes: 1

Views: 790

Answers (1)

Tim Lewis
Tim Lewis

Reputation: 29278

It looks like you have the right steps, get the inputs:

$sku = Input::get('sku');
$retailer = Input::get('retailer');
$dates = Input::get('obs');
$sales= Input::get('sales');

Buy now you try to forcibly insert them into the database. Why not use eloquent for database insertion: (Keep in mind you'd need to have a model for the charts table called Chart.php)

$chart = new Chart;
$chart->sku = $sku;
$chart->retailer = $retailer;
$chart->dates = $dates;
$chart->save();

That being said, I do realize that you're trying to pass arrays to the database, so that might take some experimentation. If you can't figure out what's (attempting) being passed to the database, you can always use:

die($variable);

To check what's up. Good luck!

Upvotes: 1

Related Questions