Reputation: 91
Hi I am trying to upload images and i keep getting an error "Call to member function store()" on null.
I have added use Illuminate\Http\UploadedFile; at the top of the file i thought it could be the issue.
please assist thanks.
Controller
public function add(Request $request)
{
$file = request()->file('avator')->store('events');
Events::Create($request->all() + ['image' => $file]);
return redirect('events');
}
view
<div class="header">
<h4 class="title">New Event</h4>
</div>
<div class="content">
{!! Form::open(['url' => '/newevent']) !!}
<div class="row">
<div class="col-md-12">
<div class="form-group">
{!! Form::label('heading', 'Heading') !!}
{!! Form::text('heading', null, ['class' => 'form-control border-input', 'placeholder' => 'Heading']) !!}
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
{!! Form::label('body', 'Body')!!}
{!! Form::textarea('body', null, ['class' => 'form-control border-input', 'placeholder' => 'Body to Events']) !!}
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
{!! Form::label('avator', 'Image')!!}
{!! Form::file('avator', ['class' => 'form-control border-input']) !!}
</div>
</div>
</div>
<div class="text-center">
{!! Form::submit('Save Me!', ['class'=> 'btn btn-info btn-fill btn-wd']) !!}
</div>
<div class="clearfix"></div>
{!! Form::close() !!}
</div>
</div>
Upvotes: 3
Views: 10086
Reputation: 17668
You forget to add 'files'=> true
in your array()
of Form::open()
function, you can do it as:
{{ Form::open(array('url' => '/newevent', 'files' => true)) }}
otherwise you can use the html form tag as:
<form action="/newevent" method="post" enctype="multipart/form-data">
Upvotes: 4
Reputation: 163798
You need to check if there is file first:
if (request()->hasFile('avator')) {
$file = request()->file('avator')->store('events');
Events::Create($request->all() + ['image' => $file]);
}
Upvotes: 0