简体   繁体   English

如何通过 user_id 使用 Laravel 导入 excel?

[英]How to pass user_id for importing excel using Laravel?

I have a problem with excel import using Laravel.我在使用 Laravel 导入 excel 时遇到问题。 The user wants import excel of suppliers data into database.用户要将供应商数据的 excel 导入数据库。 The suppliers table got user_id but the user may not have their own user_id . suppliers表有user_id但用户可能没有自己的user_id So, when importing excel file the user use excel without user_id columns.因此,在导入 excel 文件时,用户使用没有user_id列的 excel。 So I want to pass the user_id of that user and stored in database.所以我想传递该用户的user_id并存储在数据库中。

SuppliersImportController供应商进口控制器

public function store(Request $request)
{
    $user_id = auth()->user()->id;        
    Excel::import(new SuppliersImport($user_id), request()->file('file'));

    return  redirect('/suppliers')->withStatus('Excel file imported successfully');
}

SuppliersImport供应商进口

public function  __construct($user_id)
{

    $this->user_id =$user_id;
}
public function model(array $row)
{
    return new Supplier([
        'name' =>$row[0],
        'phone' =>$row[1],
        'email' =>$row[2],
        'address' =>$row[3],
        'city' =>$row[4],
        'country' =>$row[5],
        'customer' =>$row[6],
        'user_id'=> $this->user_id,         
        
    ]);

Error错误

SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into suppliers ( name , phone , email , address , city , country , customer , updated_at , created_at ) values (soe, 176893848, soe@mail.com, abc, Yangon, Myanmar, 1, 2020-11-27 12:15:08, 2020-11-27 12:15:08)) SQLSTATE [HY000]:一般错误:1364 字段 'user_id' 没有默认值(SQL:插入suppliersnamephoneemailaddresscitycountry /地区、 customerupdated_atcreated_at )值(soe、176893848 , soe@mail.com, abc, Yangon, Myanmar, 1, 2020-11-27 12:15:08, 2020-11-27 12:15:08))

So what you can do is所以你能做的是

public function model(array $row)
{    
    $user = auth()->user();  //Get the current user 
    return new Supplier([
        'name' =>$row[0],
        'phone' =>$row[1],
        'email' =>$row[2],
        'address' =>$row[3],
        'city' =>$row[4],
        'country' =>$row[5],
        'customer' =>$row[6],
        'user_id'=> $user->id,      // add their user id when importing    
        
    ]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 ajax 路由数据表 Laravel 中传递 user_id - How to pass user_id in ajax route datatable Laravel 完整性约束违规:1048 列 'user_id' 不能是 null,同时导入具有多个工作表的 excel 文件。 我正在使用“Laravel Excel”package - Integrity constraint violation: 1048 Column 'user_id' cannot be null while importing excel file with multiple sheet. Am Using “Laravel Excel” package Laravel如何使用angular使用user_id检索名称? - Laravel how to retrieve name with user_id using angular? 如何在 Laravel 中按 user_id 对数组进行分组 8 - How to group array by user_id in Laravel 8 laravel 8 API:如何将带有 Auth 的用户名作为存储帖子的 user_id 值传递 - laravel 8 API : how pass username with Auth as user_id value for store post 如何在Laravel 5.6中使用车辆user_id打印用户电子邮件 - How to print user email using vehicle user_id in Laravel 5.6 如何从Laravel中的user_id中检索用户名? - How retrive user name from user_id in laravel? 如何从laravel中的user_id获取用户名? - How to get user's name from user_id in laravel? 如何在帖子top中使用user_id访问用户名。 或在laravel中的帖子显示页面上 - How can I access username by using user_id in posts top . Or on Show page of posts in laravel 如何使用 Laravel Vue.js 在数据库中保存 user_id - How I Save user_id in Database using Laravel Vue.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM