mardi 22 décembre 2015

Updating a table in DB with csv file - Laravel 5

i have a problem with updating a table in my DB with data from my excel file in Laravel 5.I am using this library.

http://ift.tt/1kdyvgZ

When i am inserting data in DB for the first time , everything works fine , the problem comes when i change some data in excel file , and try to upload it again , it simply insert all data to DB again , and thing that i want to achieve is that only edited row or column from excel file change his value in DB and the rest just stay the same...

public function updateProductsList(Request $request)
{
    try {
        Excel::load($request->file('csv'), function ($reader) {
            foreach ($reader->all() as $row) {
                $product = new Product();
                    if(!empty(Category::ofTitle($row['category'])->first())) {
                    $category = Category::ofTitle($row['category'])->first();
                    $product = Product::firstOrCreate(['product_category' => $category->id]);
                }
                else {
                    $product = Product::firstOrCreate(['product_category' => 0]);
                }
                $product = Product::firstOrCreate(['product_name' => $row['name']]);
                $product = Product::firstOrCreate(['product_price' => $row['price']]);
                if(!empty($row['quantity']))
                $product = Product::firstOrCreate(['product_quantity' =>$row['quantity']]);
                else {
                $product = Product::firstOrCreate(['product_quantity' =>0]);
                }
                if(!empty($row['action']))
                     $product = Product::firstOrCreate(['product_onaction' =>$row['action']]);
                else {
                    $product = Product::firstOrCreate(['product_onaction' =>false]);
                }
                if($row['Condtion'] == 'New') {
                    $product = Product::firstOrCreate(['product_new' =>true]); 
                }
                else {
                     $product = Product::firstOrCreate(['product_neworused' =>true]);

                }

                if(!empty($row['delivery']))
                    $product = Product::firstOrCreate(['product_additional_info' =>$row['delivery']]); 
                else {
                    $product = Product::firstOrCreate(['product_additional_info' =>0]); 
                }
                if(!empty($row['youtube']))
                    $product = Product::firstOrCreate(['product_video' =>$row['youtube']]); 
                else {
                    $product = Product::firstOrCreate(['product_video' =>false]); 
                }
                if(!empty($row['url_pic']))
                    $product = Product::firstOrCreate(['product_picture' =>$row['url_pic']]); 
                else {
                    $product = Product::firstOrCreate(['product_picture' =>false]); 
                }
                $product->save();
            }
        });
        Session::flash('flash_message', 'You have successfuly changed product table.');
        return redirect()->back();
    } catch (\Exception $e) {
        Session::flash('error', 'Error, please try it again.');
        return redirect()->back();
    }
}

Any kind of help is appreciated.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire