Reputation: 620
I have simple Form:
<form method="post" action="{{ route('company.store') }}">
Logo: <br>
<input name="logo" type="file"><br>
In my controller i try to save image with
$file = $request->file('logo')->store('avatars');
but i have error
"Call to a member function store() on null"
dd($request->file('logo');
shows 'null'
How to reach file to save it?
Upvotes: 0
Views: 8600
Reputation: 56
uploading file need a enctype="multipart/form-data"
<form action="{{ route('company.store') }}" method="post" enctype="multipart/form-data"
class="form-material">
{{csrf_field()}}
<div class="form-body">
<h3 class="card-title">upload image</h3>
<div class="form-group">
<label class="control-label">image 1</label>
<input type="file" name="image_path" class="form-control">
</div>
</div>
</form>
Your Controller should look like this .
public function store(Request $request)
{
$this->validate($request,['image_path'=> 'required|image']);
$company = new Company();
if($request->hasFile('image_path'))
{
$company->image_path= $request->file('image_path')->store('company','public');
}
$company->save();
return back()->with('success', 'Done!');
}
Upvotes: 2
Reputation: 35180
To upload a file you need to add enctype="multipart/form-data"
to you opening form tag:
<form method="post" action="{{ route('company.store') }}" enctype="multipart/form-data">
If you don't include it then only the file name will be submitted and will cause
$request->file('logo')
to return null
because it isn't a file it's a string.
Upvotes: 4