0

I'm trying to save multiple images of my property, a property can have several images, but I get this error

I would like to know what it produces if you can help me here I leave my controller in the store function

code upload image

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'));
    }

Migration - Properties

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

        });
    }

Migration images

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

my property model

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

model images


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

I don't know if I have something wrong with my controller but it gives me the error before saving it

6
  • well if you look at your error message and stack trace it tells you what line causes the error, so then you can focus on fixing it. Would you like to provide the error the line is on or part of the stack trace? Commented Jul 2, 2020 at 19:37
  • What line specifically is throwing that error? This is a pretty large code-dump, so it would be helpful if you point us in the right direction. Sidenote, you're defining the $images array, and pushing $name values to it, but never using it beyond that. Commented Jul 2, 2020 at 19:38
  • the error throws it to me in the line of $ property-> save (); If I do a dd before it, it throws the array at me with the images Commented Jul 2, 2020 at 19:41
  • I scrolling and scrolling and at least I reached here... Commented Jul 2, 2020 at 19:43
  • May be error here $properti->images = $request->images; its not a field, its a relationship with Image model Commented Jul 2, 2020 at 19:46

1 Answer 1

3

This is assigning an array to images:

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

You are then calling $properti->save() which is trying to save that array, which it can't; it will try to convert it to a string, which it can't. So you can comment/remove this line.

If you want to save these via the relationship you can try something like this:

$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]);
}
Sign up to request clarification or add additional context in comments.

7 Comments

idk, how can you save an array of data to a single field? is that even what you actually want to do?
<input required type="file" class="form-control" name="images[]" placeholder="Imagenes" multiple> So I have my input for the images
@cesar you make a foreach to get all image as single, you can insert then all in your Image model
you will have to save these images to the images table and link them to the property, which you can do with the same loop you are currently using for the images
If I want the images table to save all the images with the property id, and so every time I create a property
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.