LaraWebDev1's avatar

What is the best way to multiple handle image upload in Laravel

Please don't recommend This Article

What is the best to handle image upload. Currently, I have it working, but I can't upload multiple, and I can't use the "path" as a URL as it's to long.

Can someone please look over my code and recommend the best approach to handle multiple images, and be image you use it in the url without a 414 error.

My code

Controller

public function store(Request $request){

  $input=$request->all();
  $images=array();
  if($files=$request->file('photo')){
    foreach($files as $file){
      $name=$file->getClientOriginalName();
      $file->move('photo', $name);
      $images[]=$name;
    }
  }

$Advert = PropertyAdvert::create([
    //"photo"       => base64_encode(file_get_contents($request->photo->path())),
    'photo'=>  implode("|",$images)
]);

$id = $Advert->id;

return redirect("/property/$id");
 }

Form

<form method="POST" action="/property" enctype="multipart/form-data">
                    {{ csrf_field() }}
                                              <div class="form-group row">
                          <label for="photo" class="col-md-3 col-form-label text-md-right">Images</label>
                          <div class="col-md-9">
                            <input required type="file" class="form-control" name="photo[]" placeholder="address" multiple>
                          </div>
                        </div>
</form>

Migration

public function up()
 {
    Schema::create('property_adverts', function (Blueprint $table) {
        $table->increments('id');
        $table->binary('photo');
        $table->timestamps();
    });
 }
0 likes
1 reply
shez1983's avatar

try

 if ( $request->file('photos') )
                {
                    foreach ($request->file('photos') as $image)
                    {

Please or to participate in this conversation.