【问题标题】:Call to a member function store() on null laravel 5.3在 null laravel 5.3 上调用成员函数 store()
【发布时间】:2017-04-08 20:56:28
【问题描述】:

您好,我正在尝试上传图片,但一直收到错误 “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>

【问题讨论】:

    标签: php laravel file-upload laravel-5.3


    【解决方案1】:

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

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

    否则你可以使用html表单标签作为:

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

    Docs

    【讨论】:

      【解决方案2】:

      需要先检查是否有文件:

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

      【讨论】:

      • 感谢@Alexey Mezenin 和@iCode。你们都帮忙解决了。
      猜你喜欢
      • 2017-03-10
      • 2020-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-06
      • 1970-01-01
      • 2020-10-28
      • 2021-04-25
      相关资源
      最近更新 更多