2017-07-03 4 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> 

私はそれを行うにはコントローラーに何をすべきかと私はポストにプレビューするか?? が

 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(); 

にあなたにすべて

+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(); 
    } 

ありがとう、あなたはちょうどあなたが複数の画像をアップロードすると、この機能は、各画像ごとに呼び出されるん

関連する問題