在 null laravel 5.3 上调用成员函数 store()
Call to a member function store() on null laravel 5.3
您好,我正在尝试上传图片,但我一直收到错误 "Call to member function store()" on null。
我添加了使用Illuminate\Http\UploadedFile;
在文件的顶部,我认为这可能是问题所在。
请帮忙谢谢。
控制器
public function add(Request $request)
{
$file = request()->file('avator')->store('events');
Events::Create($request->all() + ['image' => $file]);
return redirect('events');
}
查看
<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>
您需要先检查是否有文件:
if (request()->hasFile('avator')) {
$file = request()->file('avator')->store('events');
Events::Create($request->all() + ['image' => $file]);
}
您忘记在 Form::open()
的 array()
函数中添加 'files'=> true
,您可以这样做:
{{ Form::open(array('url' => '/newevent', 'files' => true)) }}
否则您可以使用 html 表单标签作为:
<form action="/newevent" method="post" enctype="multipart/form-data">
您好,我正在尝试上传图片,但我一直收到错误 "Call to member function store()" on null。
我添加了使用Illuminate\Http\UploadedFile; 在文件的顶部,我认为这可能是问题所在。
请帮忙谢谢。
控制器
public function add(Request $request)
{
$file = request()->file('avator')->store('events');
Events::Create($request->all() + ['image' => $file]);
return redirect('events');
}
查看
<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>
您需要先检查是否有文件:
if (request()->hasFile('avator')) {
$file = request()->file('avator')->store('events');
Events::Create($request->all() + ['image' => $file]);
}
您忘记在 Form::open()
的 array()
函数中添加 'files'=> true
,您可以这样做:
{{ Form::open(array('url' => '/newevent', 'files' => true)) }}
否则您可以使用 html 表单标签作为:
<form action="/newevent" method="post" enctype="multipart/form-data">