我一直在关注如何将多个文件附加到Laravel项目的教程here,但我一直遇到错误,将文件保存到表中。
Call to a member function store() on array
作业可以很好地保存到我的数据库中,但是我尝试保存文件时发生错误。
每当我尝试在文件上使用store方法时,我似乎都会收到此错误,我很乐意深入了解这一点。
JobController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use App\Job;
Use App\Brand;
Use App\JobType;
use App\JobFile;
class JobController extends Controller
{
...
public function store(Request $request)
{
...
$job = Job::create([
'title' => $request->title,
'brand_id' => $request->brand_id,
'job_type_id' => $request->job_type_id
]);
foreach ($request->files as $file) {
$filename = $file->store('job-files');
JobFile::create([
'job_id' => $job->id,
'filename' => $filename
]);
}
return redirect('/jobs');
}
}
create.blade.php
<form action="/jobs/create" method="POST" enctype='multipart/form-data'>
...
<div class="form-group">
<label for="file">File Upload(s)</label>
<input type="file" name="files[]" multiple>
</div>
...
</form>
答案 0 :(得分:0)
你必须先获得所有文件的数组,然后才能获得foreach。
if($request->hasFile('files')) {
$files = $request->file('files');
foreach ($files as $file) {
$filename = $file->store('job-files');
JobFile::create([
'job_id' => $job->id,
'filename' => $filename
]);
}
}