andremac96's avatar

ttemping to upload multiple photos instead of 1. Appears to upload, but I cant display them.

I have a property ad, which has a field to upload photos. It 'seems' to upload them with no errors, but I can't display them. I get the missing image icon. Here is my code

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>

Store 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),
        "address"     => $request->address,
        "county"      => $request->county,
        "town"        => $request->town,
        "type"        => $request->type,
        "rent"        => $request->rent,
        "date"        => $request->date,
        "bedrooms"    => $request->bedrooms,
        "bathrooms"   => $request->bathrooms,
        "furnished"   => $request->furnished,
        "description" => $request->description,
        "user_id" => Auth::id(),
    ]);

    $id = $Advert->id;

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

Any ideas?

EDIT

This is the migration file. As you can see the photo field is the first one.

Schema::create('property_adverts', function (Blueprint $table) {
            $table->increments('id');
            $table->binary('photo');
            $table->string('address');
            $table->string('county');
            $table->string('town');
            $table->string('type');
            $table->string('rent');
            $table->string('date');
            $table->string('bedrooms');
            $table->string('bathrooms');
            $table->string('furnished');
            $table->longText('description');
            $table->integer('user_id');
            $table->timestamps();
        

Show Page Code

<div class="card-body col-md-12">
      @foreach ($Advert as $Ad)
        <img src="data:image/gif;base64,{{ $Ad['photo'] }}" class="img-fluid full-width col-md-12">
      @endforeach
  
    </div>
0 likes
3 replies
bobbybouwmann's avatar

So first of all when you upload images you shouldn't store them in the database. Instead you should upload them to your server and store the path to that file in your database.

Also since you have multiple photos you should use a relationship between them. For example a different table for your photo that has a foreign key to the property_adverts table.

Please or to participate in this conversation.