这个函数是我用来存储新公司的函数:
public function store(Request $request)
{
$file = $request->file('logo');
$filename = 'company-logo-' . time() . '.' . $file->getClientOriginalExtension();
$path = $file->storeAs('public', $filename);
dd($path);
Company::create([
'name' => $request->name,
'email' => $request->email,
'logo' => $request->logo,
'website' => $request->website
]);
return redirect('/company/all');
}
此视图具有以下形式:
@extends('layouts.app')
@section('content')
<div class="card">
<div class="card-body">
<h4 class="card-title">Create a Company</h4>
</div>
<div class="container">
<div class="jumbotron">
<ul class="list-group">
<li class="list-group-item">
<h3>Enter Company Information:</h3>
<form action="{{ route('company.store') }}" enctype="mutlipart/form-data" method="POST">
@csrf
<div class="form-group">
<input type="text" class="form-control" name="name" placeholder="Company name" value="{{ old('name') }}">
</div>
<div class="form-group">
<input type="text" class="form-control" name="email" placeholder="Email" value="{{ old('email') }}">
</div>
<div class="form-group">
<input class="form-control" type="file" name="logo">
</div>
<div class="form-group">
<input class="form-control" type="url" name="website" placeholder="Website" value="{{ old('website') }}">
</div>
<button class="btn btn-primary" type="submit">Add Company</button>
</form>
</li>
</ul>
</div>
</div>
</div>
@endsection
这是路线:
Route::post('/add', 'CompaniesController@store')->name('store');
好吧,当我尝试提交此表单时会发生什么,$file 变量总是返回 null:
Error
Call to a member function getClientOriginalExtension() on null
http://localhost:8000/company/add
基本上我想要做的是将图像的名称发送到数据库并将图像上传到我的公共文件夹。这段代码也不会发生。当我擦除从 $file 到 dd($path) 的部分时;它将值添加到数据库中,但尚未上传图像。
有什么帮助吗?提前致谢。