导入在Laravel-Excel插件中无法正常工作

时间:2015-07-16 09:32:57

标签: php laravel-5 laravel-excel

我的应用程序中有2个表。 productsproduct_metas。两者都已建立One-To-One Relationship

我使用laravel-excel插件作为导入和导出productsproduct_metas表格的解决方案。

现在,导出功能完全正常,没有任何问题。但导入并没有按照我想要的方式运行。

这是导入的控制器方法:

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');
    }
}

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');
    }
}

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:3)

我已经解决了这个问题,我在这里发布代码供将来参考,也许它可以帮助那些正在努力争取同样解决方案的人。

我不知道这是否正确,但这就是我/我一直在寻找的:

这是控制器方法:

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');
}