我试图使用Excel文件导入作业请求的行,但不知道如何将作业请求的id存储在它的行中。
我有两个模特
作业请求
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。

到行。

这就是我需要解决的问题。
提前谢谢你。我期待着你的答复。
发布于 2021-09-29 11:08:20
我对你的措辞有点困惑,但我想这就是你想说的?
您有一个CustomJob模型(为了避免与Laravel‘jobs’混淆),它对应于数据库中的一个custom_jobs表,其中列为'id‘、'customer_id’、'note‘、'created_by’、'updated_by‘。(我不知道为什么你有一个'id‘字段,大概还有'job_id’字段,所以后者可能不相关)
每个CustomJob都有一个或多个行,这些行与CustomJob (通过lines.custom_job_id = custom_jobs.id)、用户(通过lines.user_id = users.id)和产品(通过lines.product_id = products.id)相关联。
看起来,您还需要一个与原因的关联(通过lines.reason_id = reasons.id)。
用户在站点前面提交上载表单,其中包含一个文件和一个custom_job_id。在您的LineController中,您使用的是Maatweb/Excel,从外观上看,您可以将文件传递给它,它基本上可以拆分行。您希望它返回一个新的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;
}然后,在填充行时,您可以在模型中访问它。
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,
]);
}https://stackoverflow.com/questions/69374750
复制相似问题