2017-07-03 325 views
1

我如何上傳多個圖像並使用laravel 5.3查看它?我如何上傳多個圖像,並在laravel 5.3中獲取?

我的控制器的功能是:

public function newOffer(Request $request){ 
    $validate_marge['img'] = 'required|image|mimes:png,jpg,jpeg'; 
    $validator = Validator::make($request->all(), $validate_marge); 
    if ($validator->fails()){ 
     return back()->withInput()->withErrors($validator); 
    }else{ 
     $post = new Post(); 
     if (Input::hasFile('img')){ 
      $file = Input::file('img'); 
      $file->move(public_path('img/offers').'/', $file->getClientOriginalName()); 
      $post->img = $file->getClientOriginalName(); 
     } 
    } 
    $post->title = Input::get('title'); 
    $post->category_id = Input::get('category_id'); 
    $post->second_title = Input::get('second_title'); 
    $post->description = Input::get('description'); 
    $post->location = Input::get('location'); 
    $post->price = Input::get('price'); 
    $post->save(); 
    return back(); 
} 

,是HTML的形式是:

<form class="form-horizontal" method="post" action="{{ url('newOffer') }}" 
    enctype="multipart/form-data"> 
{{ csrf_field() }} <div class="form-group"> 
    <label class="col-md-4 control-label">Select IMG's</label> 
    <div class="col-md-6"> 
     <input type="file" class="form-control" id="image" 
       name="images[]" 
       required 
       data-validation-required-message="Please choose image" multiple> 
     <p class="help-block">{{$errors->first('images')}}</p> 
    </div> 
</div> <input type="submit" value="Add" class="btn btn-primary" style="width: 100%"></form> 

我做什麼的控制器來做到這一點,我在後期如何預覽? 謝謝大家

+0

當您上傳多張圖片,這是否函數被調用爲每個圖像上傳? –

+0

你可以請添加你的表單屬性? – wahdan

+0

當您保存圖像時,您可以添加foreach,但是我有一個問題,每個帖子都有相同的圖像嗎? – wahdan

回答

1

變化

if (Input::hasFile('img')){ 
     $file = Input::file('img'); 
     $file->move(public_path('img/offers').'/', $file->getClientOriginalName()); 
     $post->img = $file->getClientOriginalName(); 
    } 

 if (Input::hasFile('img')){ 
       $files = Input::file('img'); 
       foreach ($files as $file){ 
       $file->move(public_path('img/offers').'/', $file->getClientOriginalName()); 
       } 
        $post->img = $file->getClientOriginalName(); 
        $post->title = Input::get('title'); 
        $post->category_id = Input::get('category_id'); 
        $post->second_title = Input::get('second_title'); 
        $post->description = Input::get('description'); 
        $post->location = Input::get('location'); 
        $post->price = Input::get('price'); 
        $post->save(); 

      } 
return back(); 

你只需要遍歷您上傳的文件