php - Import not working properly in Laravel-Excel plugin -


there 2 tables in application. products , product_metas. both have one-to-one relationship established.

i using laravel-excel plugin solution import , export of products , product_metas table.

now, export function working fine without issue. import not working way want.

here's controller method import:

public function postimportproducts(request $request) {     excel::load( $request->file( 'productsfile' ), function ( $reader ) {             $reader->each( function ( $sheet ) {              if ( $sheet->gettitle() === 'product-general-table' ) {                  $sheet->each( function ( $row ) {                     echo $row->name . '<br />'; // <-- outputs name correctly.                      db::statement( 'set foreign_key_checks=0;' );                      db::table( 'products' )->truncate();                      $product = new product;                     $product->name = $row->name;                     $product->amount = $row->amount;                     $product->save();                      db::statement( 'set foreign_key_checks=1;' );                 });             }              if ( $sheet->gettitle() === 'product-meta-table' ) {                  $sheet->each( function ( $row ) {                      db::statement( 'set foreign_key_checks=0;' );                      db::table( 'product_metas' )->truncate();                      $productmeta = new productmeta;                     $productmeta->product_id = $row->product_id;                     $productmeta->description = $row->description;                     $productmeta->title = $row->title;                     $productmeta->keywords = $row->keywords;                     $productmeta->save();                      db::statement( 'set foreign_key_checks=1;' );                 });             }          });     }); } 

i don't know mistake, following errorexception:

sqlstate[23000]: integrity constraint violation: 1048 column 'name' cannot null (sql: insert `products` (`name`, `amount`, `updated_at`, `created_at`) values (, , 2015-07-16 09:18:02, 2015-07-16 09:18:02)) 

edit 1:

product model:

<?php  namespace app;  use illuminate\database\eloquent\model;  class product extends model {     /**      * properties mass assignable      *      * @var array      */     protected $fillable = ['name', 'amount'];      public function categories()     {         return $this->belongstomany('app\category');     }      public function meta()     {         return $this->hasone('app\productmeta');     } } 

productmeta model

<?php  namespace app;  use illuminate\database\eloquent\model;  class productmeta extends model {     protected $fillable = ['product_id', 'title', 'description', 'keywords'];      public function product()     {         return $this->belongsto('app\product');     } } 

kindly me out this.

i have solved , posting code here future reference, perhaps can others struggling same solution.

i don't know if correct way, / looking for:

here's controller method:

public function postimportproducts(request $request) {     $getsheetname = excel::load($request->file('productsfile'))->getsheetnames();      foreach($getsheetname $sheetname)     {         if ($sheetname === 'product-general-table')         {             db::statement('set foreign_key_checks=0;');              db::table('products')->truncate();              excel::selectsheets($sheetname)->load($request->file('productsfile'), function ($reader)             {                 foreach($reader->toarray() $sheet)                 {                     product::create($sheet);                 }             });              db::statement('set foreign_key_checks=1;');              //var_dump('product general done');         }          if ($sheetname === 'product-meta-table')         {              // dd('loading meta');              db::statement('set foreign_key_checks=0;');              db::table('product_metas')->truncate();              excel::selectsheets($sheetname)->load($request->file('productsfile'), function ($reader)             {                     foreach($reader->toarray() $sheet)                 {                     productmeta::create($sheet);                 }             });              db::statement('set foreign_key_checks=1;');             //var_dump('product meta done');         }     }      session::flash('file_uploaded_successfully', 'file has been uploaded , has updated database.');      return redirect('/import-products'); } 

Comments