dshgdhdfcas30210 2015-07-16 09:32
浏览 119
已采纳

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

There are 2 tables in my application. products and product_metas. Both have One-To-One Relationship established.

I am using laravel-excel plugin as a solution for import and export of my products and product_metas table.

Now, the export function is working completely fine without any issue. But the import is not working the way I want.

Here's the controller method for 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 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;' );
                });
            }

        });
    });
}

I don't know what is the mistake, but I get the following 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))

EDIT 1:

Product Model:

<?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 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 help me out with this.

  • 写回答

1条回答 默认 最新

  • dongtao4787 2015-07-16 11:01
    关注

    I have solved it and I am posting the code here for future reference, perhaps it can help others who are struggling for the same solution.

    I don't know if this is the correct way, but this is what I am / I was looking for:

    Here's the controller method:

    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');
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮