【问题标题】:How to import excel file with relationship id?如何导入带有关系ID的excel文件?
【发布时间】:2026-02-18 21:40:01
【问题描述】:

我尝试使用 Excel 文件导入作业请求的行,但我不知道如何将作业请求的 ID 存储在它的行中。

我有 2 个模型

工作申请

 protected $table = "jobs";
    protected $fillable=['job_id', 'customer_id', 'note', 'created_by', 'updated_by'];

    public function products(){
        return $this->hasMany(Product::class);
    }

    public function users(){
        return $this->belongsTo('App\User', 'created_by');
    }

    public function lines(){
        return $this->hasMany(Line::class);
    }

线

    protected $table = 'lines';
    protected $fillable=['job_id', 'product_id', 'user_id', 'reason_id', 'created_by', 'updated_by', 'created_at', 'updated_at' ];

    public function users(){
        return $this->belongsTo('App\User', 'user_id');
    }

    public function jobs(){
        return $this->belongsTo(Job::class, 'job_id');
    }

    public function products(){
        return $this->belongsTo(Product::class, 'product_id');
    }

当我添加行时,我将打开工作请求并添加行。该行将保存为 product_id 和 job_id。

                                                <form action="{{ route('file-import') }}" method="POST" class="form-default" enctype="multipart/form-data">


                                                    @csrf

                                                    <div class="col-lg-12">
                                                        <div class="card">
                                                            <!--div class="card-header">
                                                                <h4 class="card-title">Upload Excel File</h4>
                                                            </div--><!--end card-header-->
                                                            <div class="card-body">
                                                                <div class="mt-3">
                                                                    <div class="col-lg-12 mb-2 mb-lg-0">
                                                                        <label class="form-label" for="pro-end-date">Products</label>

                                                                        <label for="file">File:</label>
                                                                        <input id="file" type="file" name="file" class="form-control">
                                                                        <input type="hidden" name="job_id" value="{{ $jobs->id }}" />

                                                                    </div><!--end col-->
                                                                </div>
                                                            </div> <!-- end card-body -->
                                                        </div> <!-- end card -->
                                                    </div> <!-- end col -->

                                                    <div class="modal-footer">
                                                        <button type="submit" class="btn btn-soft-primary btn-sm">Save</button>
                                                        <button type="button" class="btn btn-soft-secondary btn-sm" data-bs-dismiss="modal">Cancel</button>
                                                    </div><!--end modal-footer-->


                                                </form>

这是 LineController

    public function fileImport(Request $request)
    {

        $file = $request->file;

        Excel::import(new LineImport, $file);

        return back();
    }

这是 LineImport 模型

    public function model(array $row)
    {
        $job = Job::find(1);

        return new Line([
            'product_id' => $row['product_id'],
            'reason_id' => $row['reason_id'],
            'updated_by' => Auth::id(),
            'job_id' => $job->id,
        ]);
    }

我不知道如何存储job_id(这个job_id是我在上传Excel文件之前打开的记录)。

在 Excel 文件中使用 product_id

这是我需要解决方案来解决的问题。

提前谢谢你。期待您的回复。

【问题讨论】:

  • 我肯定会考虑不使用“jobs”作为自定义表的名称。 "jobs" 和 "failed_jobs" 是 Laravel 的队列工作人员传统上使用的表。
  • 谢谢您的建议,先生。我将更改表的名称。

标签: laravel eloquent laravel-7 maatwebsite-excel laravel-relations


【解决方案1】:

我对你的措辞有点困惑,但我认为这就是你的意思?

你有一个 CustomJob 模型(我们称它为避免与 Laravel 'jobs' 混淆),它对应于数据库中的 custom_jobs 表,包含 'id'、'customer_id'、'note'、'created_by 列','updated_by'。 (我不确定为什么你有一个“id”字段,大概还有一个“job_id”字段,所以后者可能不相关)

每个 CustomJob 都有一个或多个 Lines,这些 Lines 与一个 CustomJob(通过lines.custom_job_id = custom_jobs.id)、一个用户(通过lines.user_id = users.id)和一个产品(通过lines.product_id = products .id)。

您似乎还需要与 Reasons 关联(通过 lines.reason_id =reasons.id)。

用户在网站前端提交上传表单,其中包含一个文件和一个 custom_job_id。在您的 LineController 中,您使用的是 Maatweb/Excel,从外观上看,您将文件传递给该文件,这基本上将行拆分。你希望它返回一个 new Line() 但你不能,因为 Line() 需要一个 custom_job_id 而你没有给它?

如果是这种情况,那么 (a) 您需要从提交的表单中获取 custom_job_id,然后 (b) 将其传递给 Import 以便它可以使用它:

从提交的表单中获取 custom_job_id

这很容易:

$file = $request->file;
$custom_job_id = $request->job_id;

将 custom_job_id 传递给导入

首先您需要编辑您的 LineController 以发送 custom_job_id :

Excel::import(new LineImport($custom_job_id), $file);

然后您需要向 LineImport 添加一个构造函数,以便它知道期待一个 custom_job_id 进来:

public function  __construct(string $custom_job_id)) {
    $this->custom_job_id= $custom_job_id;
}

然后您可以在填充 Line 时在模型中访问它

public function model(array $row)
{
    return new Line([
        'product_id' => $row['product_id'],
        'reason_id' => $row['reason_id'],
        'updated_by' => Auth::id(),
        'custom_job_id' => $this->custom_job_id,
    ]);
}

【讨论】:

  • 请注意,如果您的关系设置正确,您的模型将需要更多信息来填充它 - 您并没有按照目前的情况为其提供 user_id。
  • 我从你那里学到了新东西。非常感谢您,先生。我已经用你的建议解决了我的问题。
  • 抱歉我的英语不好。
  • 没问题,很高兴它有帮助。