vendredi 5 février 2016

upload a csv to a laravel controller and insert data to two databases

So I am a complete noob to Laravel and am trying something here. I want to import a csv file into two databases, I have a database called lists that will get the list name and a client_id

Then I have a database called customers that will get name surname contact number as well as client_id and a list_id.

What I want to achieve is to import a csv file that will take the file name and store it in the list database, then create an array through the csv file and import the data into the customers database with the list and client id's.

I tried to do it below as I found this on stack overflow but I had a feeling I would need more than this. My question is, how does one do something like this? Am I far off?

I get this error RuntimeException in AbstractCsv.php line 234: SplFileObject::__construct(mycsv.csv): failed to open stream: No such file or directory

<?php

namespace App\Http\Controllers;

use DB;
use Illuminate\Http\Request;
use App\Http\Requests\ListsRequest;
use App\Lists;
use App\Clients;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class ListsController extends Controller
{
    public function index()
    {
//        $list_items = Lists::all();
        $clients = Clients::all();

        return view('lists.show', compact('clients'));

    }

    public function store(Requests\ListsRequest $request)
    {

        $input = $request->input();

        Lists::create($input);

        $filename = $request->input('name');
        $csv = \League\Csv\Reader::createFromPath($filename);
        DB::table('customers')->insert( $csv->fetchAssoc() );

        return view('clients');

    }
}



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1mhF9XO
via IFTTT

Aucun commentaire:

Enregistrer un commentaire