【发布时间】: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模型有关系