导入在 Laravel-Excel 插件中无法正常工作
Import not working properly in Laravel-Excel plugin
我的申请中有 2 个 table。 products
和 product_metas
。两者都已 One-To-One Relationship
成立。
我正在使用 laravel-excel 插件作为导入和导出我的 products
和 product_metas
table.
的解决方案
现在,导出功能完全正常,没有任何问题。但是导入没有按照我想要的方式工作。
这是导入控制器的方法:
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 the 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;' );
});
}
});
});
}
我不知道是什么错误,但我得到了以下内容 ErrorException
:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null (SQL: insert into `products` (`name`, `amount`, `updated_at`, `created_at`) values (, , 2015-07-16 09:18:02, 2015-07-16 09:18:02))
编辑 1:
产品型号:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
/**
* Properties that are mass assignable
*
* @var array
*/
protected $fillable = ['name', 'amount'];
public function categories()
{
return $this->belongsToMany('App\Category');
}
public function meta()
{
return $this->hasOne('App\ProductMeta');
}
}
产品元模型
<?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');
}
}
请帮我解决这个问题。
我已经解决了,我把代码贴在这里以供将来参考,也许它可以帮助其他正在努力寻找相同解决方案的人。
我不知道这是否正确,但这就是我/我正在寻找的:
控制器方法如下:
public function postImportProducts(Request $request)
{
$getSheetName = Excel::load($request->file('productsFile'))->getSheetNames();
foreach($getSheetName as $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() as $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() as $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 successfully and has also updated the database.');
return redirect('/import-products');
}
我的申请中有 2 个 table。 products
和 product_metas
。两者都已 One-To-One Relationship
成立。
我正在使用 laravel-excel 插件作为导入和导出我的 products
和 product_metas
table.
现在,导出功能完全正常,没有任何问题。但是导入没有按照我想要的方式工作。
这是导入控制器的方法:
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 the 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;' );
});
}
});
});
}
我不知道是什么错误,但我得到了以下内容 ErrorException
:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null (SQL: insert into `products` (`name`, `amount`, `updated_at`, `created_at`) values (, , 2015-07-16 09:18:02, 2015-07-16 09:18:02))
编辑 1:
产品型号:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
/**
* Properties that are mass assignable
*
* @var array
*/
protected $fillable = ['name', 'amount'];
public function categories()
{
return $this->belongsToMany('App\Category');
}
public function meta()
{
return $this->hasOne('App\ProductMeta');
}
}
产品元模型
<?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');
}
}
请帮我解决这个问题。
我已经解决了,我把代码贴在这里以供将来参考,也许它可以帮助其他正在努力寻找相同解决方案的人。
我不知道这是否正确,但这就是我/我正在寻找的:
控制器方法如下:
public function postImportProducts(Request $request)
{
$getSheetName = Excel::load($request->file('productsFile'))->getSheetNames();
foreach($getSheetName as $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() as $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() as $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 successfully and has also updated the database.');
return redirect('/import-products');
}