0

我尝试使用 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>

这是线控制器

    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

在此处输入图像描述

在此处输入图像描述

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

先感谢您。我期待着您的回复。

4

1 回答 1

1

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

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

每个 CustomJob 都有一个或多个 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 传递给 Import

首先,您需要编辑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,
    ]);
}
于 2021-09-29T11:08:20.237 回答