簡體   English   中英

在null laravel 5.3上調用成員函數store()

[英]Call to a member function store() on null 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>

你忘記在Form::open()函數的array()中添加'files'=> true ,你可以這樣做:

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

否則你可以使用html表單標簽:

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

文件

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM