Laravel-上传图像和视频数组

时间:2018-11-20 06:40:52

标签: php mysql arrays laravel

我想上传一个图像和一个视频,这是我的逻辑,但似乎不起作用,它仅上传一个图像。我如何上传2个单独的数据。请引导我,我是Laravel新手,谢谢您

这里是View,它具有我在Form Collectives中使用过的表单 这是数组被调用的地方

  

我的观点

{!! Form::open(['action'=>'Admin\PaxSafetyController@store', 'method' => 'POST','enctype'=>'multipart/form-data', 'name' => 'add_name', 'id' => 'add_name']) !!}
<div class="form-group">   
    <div class="table-responsive">  
        <table class="table table-bordered" id="dynamic_field">  
           <tr>  
              <td>  {{ Form::file('paxsafety_image[]') }} &nbsp;&nbsp; <strong>Upload Image</strong>
                    <br><br>
                    {{ Form::file('paxsafety_video[]') }} &nbsp;&nbsp; <strong>Upload Video</strong>


              </td>
              <td>{{ Form::button('', ['class' => 'btn btn-success fa fa-plus-circle', 'id'=>'add','name'=>'add', 'style'=>'font-size:15px;']) }}</td>
           </tr>  
        </table>  
        {{Form::submit('submit', ['class'=>'btn btn-primary', 'name'=>'submit'])}}
    </div> 
</div>  
{!! Form::close() !!}
  

我的控制器

 public function create()
{
    $this->validate($request, [
        'paxsafety_image' => 'required',
        'paxsafety_video' => 'required'
    ]);

    if ($request->has('paxsafety_image'))
    {   
        //Handle File Upload

        $paxSafety = [];
        foreach ($request->file('paxsafety_image') as $key => $file)
        {
            // Get FileName
            $filenameWithExt = $file->getClientOriginalName();
            //Get just filename
            $filename = pathinfo( $filenameWithExt, PATHINFO_FILENAME);
            //Get just extension
            $extension = $file->getClientOriginalExtension();
            //Filename to Store
            $fileNameToStore = $filename.'_'.time().'.'.$extension;
            //Upload Image
            $path = $file->storeAs('public/paxsafety_folder',$fileNameToStore);
            array_push($paxSafety, $fileNameToStore);
        }

        $fileNameToStore = serialize($paxSafety);
    }


    foreach ($paxSafety as $key => $value) {
        $paxSafetyContent = new PaxSafety;
        $paxSafetyContent->paxsafety_image = $value;
        $paxSafetyContent->save();
    }
    return redirect('/admin/airlineplus/inflightmagazines/create')->with('success', 'Inflight Magazine Content Inserted');
}

0 个答案:

没有答案