我正在努力找出做某事的最佳方法。我有一个包含很多字段的表单,包括一个允许上传多个文件的文件输入。
所有这些都与模型/表单ReportingDoc
{!! Form::model(new App\ReportingDoc, [
'class'=>'form-horizontal',
'route' => ['projects.reportingDoc.store', $project->id],
'files' => true
]) !!}
<div class="form-group">
{!! Form::label('workType', 'Work Type:', array('class' => 'col-sm-5 control-label blue')) !!}
<div class="col-sm-7">
<select class="workType" name="workType">
<option value="recurring">Recurring</option>
<option value="adHoc">Ad-hoc</option>
</select>
</div>
</div>
//Lots of other inputs
<div class="form-group">
{!! Form::label('filePath', 'Supporting Documents:', array('class' => 'col-md-5 control-label green')) !!}
<div class="col-md-7">
{!! Form::file('filePath[]', array('multiple'=>true)) !!}
</div>
</div>
<div class="form-group">
{!! Form::submit('Save Data', ['class' => 'btn btn-primary']) !!}
</div>
{!! Form::close() !!}
该模型如下所示:
class ReportingDoc extends Model
{
protected $table = 'reporting_doc';
protected $guarded = [];
public function project()
{
return $this->belongsTo('App\Project', 'projectId');
}
}
因此该模型属于项目。目前,迁移如下所示:
public function up()
{
Schema::create('reporting_doc', function(Blueprint $table)
{
$table->increments('id');
$table->String('workType')->default('');
//all my other inputs
$table->String('filePath')->default('');
$table->timestamps();
});
Schema::table('reporting_doc', function (Blueprint $table) {
$table->integer('projectId')->unsigned()->default(0);
$table->foreign('projectId')->references('id')->on('projects')->onDelete('cascade');
});
}
在这条路线的控制器中,我有我的商店方法。目前它看起来如下:
public function store(Request $request, Project $project)
{
$workType = Input::get('workType');
//Other inputs
$projectId = $project->id;
$reportingDoc = new ReportingDoc();
$reportingDoc->workType = $workType;
//Other inputs
$reportingDoc->projectId = $projectId;
$dsReportingDoc->save();
return Redirect::route('projects.reportingDoc.create', $project->id)
->with('message', 'Files uploaded.');
/* Old part
$files = Input::file('filePath');
$file_count = count($files);
$uploadcount = 0;
if(isset($files)) {
foreach($files as $file) {
$destinationPath = public_path() .'/uploads/';
$filename = $file->getClientOriginalName();
$upload_success = $file->move($destinationPath, $filename);
$uploadcount ++;
}
}
if($uploadcount == $file_count){
Session::flash('success', 'Upload successfully');
return Redirect::route('projects.reportingDoc.create', $project->id)
->with('message', 'Files uploaded.');
}
else {
return Redirect::route('projects.reportingDoc.create', $project->id)
->with('message', 'Something went wrong - Please try again.');
}
*/
}
如您所见,我现在已经注释掉了文件部分。我要做的是获取所有上传文件的路径,并将它们序列化 数据库中的一个字段 - filePath。
然而,对我而言,这似乎非常混乱。我想最好有一个类似上传表的链接到这个模型的东西。然后,我可以为每个上传的文件创建一个上传对象。
我感到困惑的一件事是表单,它与我的ReportingDoc
模型相关联。有了这种新方法,我是否需要以某种方式将它链接到两个模型? ReportingDoc
和Uploads
?
真的,我只是在寻找有关实现目标的最佳途径的建议和指导。
非常感谢
答案 0 :(得分:1)
你已经差不多完成了。执行您尝试实现的最佳方法是创建属于 ReportingDocUpload
的{{1}}模型,然后从Reporting Doc中删除filePath字段。< / p>
您的模型应如下所示:ReportingDoc
然后保存文件,执行以下操作:
class ReportingDocUpload { ... protected $fillable = ['filepath', 'reportingdoc_id']; ... }
请勿忘记在删除条目时从磁盘中删除文件...
if($file_count) {
foreach($files as $file) {
$destinationPath = public_path() .'/uploads/';
$filename = $file->getClientOriginalName();
$upload_success = $file->move($destinationPath, $filename);
$rUpload = ReportingDocUpload::create(['filepath' => $filename, 'reportingdoc_id' => $reportingDoc->id]);
$uploadcount ++;
}
}
干杯..