2016-11-24 58 views
3

嗨我想上传图片,我不断收到错误“调用成员函数存储()”为空。调用成员函数商店()null laravel 5.3

我已添加使用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

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

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

否则,你可以使用HTML表单标签为:

Docs

0

您需要检查是否有文件第一:

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

感谢@阿列克谢Mezenin和@iCode。你们都协助解决这个问题。 –

相关问题