在我的 laravel 5.2 应用程序中,我有文件附件表单并将信息保存在文件表中。
文件/form.blade.php
<form class="form-vertical" role="form"
enctype="multipart/form-data"
method="post"
action="{{ route('projects.files', ['projects' => $project->id]) }}">
<div class="form-group{{ $errors->has('file_name') ? ' has-error' : '' }}">
<input type="file" name="file_name" class="form-control" id="file_name">
@if ($errors->has('file_name'))
<span class="help-block">{{ $errors->first('file_name') }}</span>
@endif
</div>
<div class="form-group">
<button type="submit" class="btn btn-info">Add Files</button>
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
文件 Controller
public function uploadAttachments(Request $request, $id,$taskId)
{
$this->validate($request, [
'file_name' => 'required|mimes:jpeg,bmp,png,pdf|between:1,7000',
]);
$filename = $request->file('file_name')->getRealPath();
Cloudder::upload($filename, null);
list($width, $height) = getimagesize($filename);
$fileUrl = Cloudder::show(Cloudder::getPublicId(), ["width" => $width, "height" => $height]);
$this->saveUploads($request, $fileUrl, $id,$taskId);
return redirect()->back()->with('info', 'Your Attachment has been uploaded Successfully');
}
private function saveUploads(Request $request, $fileUrl, $id,$taskId)
{
$file = new File;
$file->file_name = $request->file('file_name')->getClientOriginalName();
$file->file_url = $fileUrl;
$file->project_id = $id;
$file->task_id = $taskId;
$file->save();
}
现在我在 View 文件夹projects/show.blade.php 的项目文件中的show.blade.php 中有任务表单
<form method="post" action="{{ route('projects.tasks.create', $project->id) }}">
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<input type="text" name="name" class="form-control" id="name" placeholder="Add Task" value="{{ old('name') ?: '' }}">
@if ($errors->has('name'))
<span class="help-block">{{ $errors->first('name') }}</span>
@endif
</div>
@endif
<br>
<div style='display:none'; class='somename'>
<div class="form-group">
<textarea name='body'class="form-control">{{ old('body') }}</textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-default">Save</button>
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
@include('files.form') //include File.form
这是我的文件表结构
id file_name file_url project_id
当我打开任务数据输入表单时,我也可以看到文件输入表单,但现在我需要将任务 ID 输入到与每个任务相关的文件表中。我怎样才能做到这一点?
最佳答案
首先,您的“files/form.blade.php”文件正在执行 POST 而不是 GET,因此您需要像为 file_name 所做的那样包含 taskid 的输入。 也许:
<input type="hiden" value="ID" /> //ID
<input type="hiden" value="taskID" /> //TASK ID
然后在文件 Controller > uploadAttachments() 中,您需要从帖子中获取值。
$filename = $request->file('file_name')->getRealPath();
$id = $request->input('id');
$taskID = $request->input('taskID');
之后你可以调用saveUploads();用于保存。
..或者如果您发布带有参数的路由,则在表单操作中包含您的任务ID。
{{ route('projects.files', ['projects' => $project->id, 'taskid' => 'IdHere']) }}
关于php - 如何在 Laravel 5.2 中将 taskId 插入到文件表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46986657/