Search code examples
phplaravelfile-uploadlaravel-5.3

Call to a member function store() on null laravel 5.3


Hi I am trying to upload images and i keep getting an error "Call to member function store()" on null.

I have added use Illuminate\Http\UploadedFile; at the top of the file i thought it could be the issue.

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>

Solution

  • You forget to add 'files'=> true in your array() of Form::open() function, you can do it as:

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

    otherwise you can use the html form tag as:

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

    Docs