【问题标题】:Array to string conversion Laravel upload multiples images数组到字符串的转换 Laravel 上传多张图片
【发布时间】:2020-10-23 11:46:54
【问题描述】:

我正在尝试保存我的属性的多个图像,一个属性可以有多个图像,但我收到此错误

如果你能在这里帮助我,我想知道它会产生什么我将控制器留在存储功能中

代码上传图片

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

迁移 - 属性

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

        });
    }

迁移图片

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

我的房产模型

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

模型图像


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

我不知道我的控制器是否有问题,但它在保存之前给了我错误

【问题讨论】:

  • 好吧,如果您查看错误消息和堆栈跟踪,它会告诉您导致错误的行,因此您可以专注于修复它。您想提供该行所在的错误还是堆栈跟踪的一部分?
  • 具体是哪一行引发了该错误?这是一个相当大的代码转储,所以如果你指出我们正确的方向会很有帮助。旁注,您正在定义 $images 数组,并将 $name 值推送给它,但除此之外永远不要使用它。
  • 错误在 $ property-> save ();如果我在它之前做一个 dd ,它会用图像向我抛出数组
  • 我滚动和滚动,至少我到达了这里......
  • 这里可能出错$properti->images = $request->images;它不是一个字段,它与Image模型有关系

标签: php html laravel eloquent


【解决方案1】:

这是给images分配一个数组:

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

然后您正在调用 $properti->save(),它试图保存该数组,但它不能;它会尝试将其转换为字符串,但它不能。所以你可以评论/删除这一行。

如果你想通过关系保存这些,你可以尝试这样的事情:

$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]);
}

【讨论】:

  • idk,如何将数据数组保存到单个字段?这就是你真正想做的吗?
  • 所以我输入了图片
  • @cesar 您创建了一个 foreach 以将所有图像作为单个图像,然后您可以将所有图像插入您的图像模型中
  • 您必须将这些图像保存到 images 表并将它们链接到属性,您可以使用当前用于图像的相同循环来执行此操作
  • 如果我想让 images 表保存所有带有属性 id 的图像,所以每次创建属性时
猜你喜欢
  • 2021-02-21
  • 2020-12-12
  • 1970-01-01
  • 2019-09-05
  • 2018-04-28
  • 2021-12-22
  • 1970-01-01
  • 1970-01-01
  • 2016-07-08
相关资源
最近更新 更多