在null laravel 5.3上调用成员函数store()

时间:2016-11-24 14:01:46

标签: php laravel file-upload laravel-5.3

您好我正在尝试上传图片,并且我一直收到错误“调用成员函数库()”为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>

2 个答案:

答案 0 :(得分:3)

您忘了在'files'=> true array()函数中添加Form::open(),可以这样做:

{{ Form::open(array('url' => '/newevent', 'files' => true)) }}

否则您可以将html表单标记用作:

<form action="/newevent" method="post" enctype="multipart/form-data">

Docs

答案 1 :(得分:0)

您需要先检查是否有文件:

if (request()->hasFile('avator')) {
    $file = request()->file('avator')->store('events');
    Events::Create($request->all() + ['image' => $file]);
}