Saya mencoba menyimpan banyak gambar properti saya, sebuah properti dapat memiliki beberapa gambar, tetapi saya mendapatkan kesalahan ini

Saya ingin tahu apa yang dihasilkannya jika Anda dapat membantu saya di sini Saya meninggalkan pengontrol saya di fungsi toko

Kode unggah gambar

public function store(Request $request)

    {
      /*--from this session you start to save the properties with all their attributes  --*/
      
        $properti = new Propertie;

        $detail = new Detail;

        $detail->antiquity = $request->antiquity;
        $detail->furnished = $request->furnished;
        $detail->floor = $request->floor;

        $detail->save();

        $properti->details_id = $detail->id;

        $properti->name = $request->name;
        $properti->price = $request->price;
        $properti->description = $request->description;


        $properti->departaments_id = $request->departaments;
        $properti->municipalities_id = $request->municipalities;

        $properti->property_type_id = $request->type_property;
        $properti->offer_type_id = $request->type;


        $properti->details_id = $detail->id;
        $properti->images = $request->images;
        $properti->lat = $request->lat;
        $properti->lng = $request->lng;
        $properti->address = $request->address;


        if (isset($request->property_id)) {
            $property_type = $request->property_id;
        } else {
            $property_type = null;
        }
        
         $images=array();
        if($files=$request->file('images')){
        foreach($files as $file){
            $name=$file->getClientOriginalName();
            $file->move('image',$name);
            $images[]=$name;
            }
        }

        $properti->save();


        $piso_id = $properti->id;
        $space = new Space;

        $space->property_id = $piso_id;
        $space->bedrooms = $request->bedrooms;
        $space->bathrooms = $request->bathrooms;
        $space->parking = $request->parking;
        $space->area = $request->area;


        $space->save();

        $properti->spaces_id = $space->id;


        foreach ($request->input('characteristic') as $characteristic) {

            $charc = new Characteristic;

            $charc->property_id = $piso_id;
            $charc->characteristic = $characteristic;
            $charc->save();
        }    

        Session::flash('message', 'Se ha registrado su propiedad De forma exitosa');

        return redirect()->action('PropertyController@index');
        // return view('properties.index',compact('properties'));
    }

Migrasi - Properti

 public function up()
    {
        Schema::create('properties', function (Blueprint $table) {
            $table->id();
            $table->string('name')->nullable;
            $table->string('price')->nullable;
            $table->text('description')->nullable;
            $table->unsignedBigInteger('property_type_id')->nullable();
            $table->unsignedBigInteger('offer_type_id')->nullable();
            $table->unsignedBigInteger('spaces_id')->nullable();
            $table->unsignedBigInteger('departaments_id')->nullable();
            $table->unsignedBigInteger('municipalities_id')->nullable();
            $table->unsignedBigInteger('details_id')->nullable();
            //$table->unsignedBigInteger('characteristics_id')->nullable();

            $table->unsignedBigInteger('images_id')->nullable();
            $table->decimal('lat', 8, 5)->nullable;
            $table->decimal('lng', 8, 5)->nullable;
            $table->string('address')->nullable;
            
            $table->timestamps();
            
            $table->foreign('property_type_id')->references('id')->on('property_type');
            $table->foreign('offer_type_id')->references('id')->on('offer_type');
            $table->foreign('spaces_id')->references('id')->on('spaces');
            $table->foreign('departaments_id')->references('id')->on('departaments');
            $table->foreign('municipalities_id')->references('id')->on('municipalities');
            $table->foreign('details_id')->references('id')->on('details');
            $table->foreign('images_id')->references('id')->on('images');
           //$table->foreign('characteristics_id')->references('id')->on('characteristics')->onDelete('cascade');

        });
    }

Gambar migrasi

 public function up()
    {
        Schema::create('images', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->unsignedBigInteger('property_id');
            $table->timestamps();
        });
    }

Model properti saya

  public function Images()
    {
        return $this->hasMany('App\Image', 'images_id');
    }

Gambar model


class Image extends Model
{
    protected $fillable = ['name', 'property_id'];
    
   public function properties()
    {
     return $this->belongsTo('App\Properties');
    }
}

Saya tidak tahu apakah saya memiliki sesuatu yang salah dengan pengontrol saya tetapi itu memberi saya kesalahan sebelum menyimpannya

0
cesar zorrilla 2 Juli 2020, 22:31

2 jawaban

Saya mendapatkan kesalahan

ErrorException
Undefined variable: files
0
cesar zorrilla 6 Juli 2020, 20:46

Ini menugaskan array ke images:

$properti->images = $request->images;

Anda kemudian memanggil $properti->save() yang mencoba menyimpan array itu, yang tidak bisa; itu akan mencoba mengubahnya menjadi string, yang tidak bisa. Jadi Anda dapat mengomentari/menghapus baris ini.

Jika Anda ingin menyimpan ini melalui hubungan, Anda dapat mencoba sesuatu seperti ini:

$properti->save();

foreach($files as $file) {
    $name=$file->getClientOriginalName();
    $file->move('image',$name);
    
    // create a new Image and relate it to the property
    $properti->images()->create(['name' => $name]);
}
2
lagbox 2 Juli 2020, 19:57