在 null laravel 5.3 上调用成员函数 store()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40788349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call to a member function store() on null laravel 5.3
提问by Lenny Kamande
Hi I am trying to upload images and i keep getting an error "Call to member function store()" on null.
嗨,我正在尝试上传图像,但在 null 上不断收到错误“调用成员函数 store()”。
I have added use Illuminate\Http\UploadedFile; at the top of the file i thought it could be the issue.
我添加了 use Illuminate\Http\UploadedFile; 在文件的顶部,我认为这可能是问题所在。
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>
采纳答案by Amit Gupta
You forget to add 'files'=> true
in your array()
of Form::open()
function, you can do it as:
你忘记添加'files'=> true
你array()
的Form::open()
函数,你可以这样做:
{{ Form::open(array('url' => '/newevent', 'files' => true)) }}
otherwise you can use the html form tag as:
否则,您可以将 html 表单标记用作:
<form action="/newevent" method="post" enctype="multipart/form-data">
回答by Alexey Mezenin
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]);
}