试图获得非对象 Laravel 的属性“浴室”
Posted
技术标签:
【中文标题】试图获得非对象 Laravel 的属性“浴室”【英文标题】:Trying to get property 'bathrooms' of non-object Laravel 【发布时间】:2020-06-26 22:32:52 【问题描述】:好我有以下错误我不明白为什么,如果我所有的关系都很好,据我所知..我想知道他们是否发现任何错误,以便他们不会向我展示我的财产空格
属性模型
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Propertie extends Model
//
public function characteristic()
return $this->HasMany('App\Characteristic', 'property_id');
public function departament()
return $this->belongsTo('App\Departament');
public function municipality()
return $this->belongsTo('App\Detail');
public function detail()
return $this->belongsTo('App\Detail');
public function offer_type()
return $this->belongsTo('App\Offer_type','offer_type_id');
public function property_type()
return $this->hasOne('App\Property_type','id','property_type_id');
public function space()
return $this->belongsTo('App\Space', 'spaces_id');
public function ImgProperties()
return $this->hasMany('App\ImgProperties');
protected $fillable = [
'name', 'description', 'property_type_id', 'offer_type', 'spaces_id', 'departaments_id',
'municipalities_id', 'details_id', 'characteristics_id', 'images','url', 'lat', 'lng','direction','price'
];
迁移属性
public function up()
Schema::create('properties', function (Blueprint $table)
$table->increments('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->string('images')->nullable;
$table->float('lat')->nullable;
$table->float('lng')->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('characteristics_id')->references('id')->on('characteristics')->onDelete('cascade');
);
空间迁移
*/
public function up()
Schema::create('spaces', function (Blueprint $table)
$table->id();
$table->integer('property_id')->references('id')->on('properties')->onDelete('cascade');
$table->string('bedrooms');
$table->string('bathrooms');
$table->string('parking');
$table->string('area');
$table->timestamps();
);
索引方法
public function index(Request $request)
$name = $request->get('name');
if($request->p_type)
$properties = Propertie::typeof($request->p_type);
elseif($request->sorttype)
$properties = Propertie::sorttype($request->sorttype);
else
$properties = Propertie::with('space')->orderBy('id', 'ASC')
->name($name)
->paginate(5);
$property_type = Property_type::all();
$spaces = Space::all();
return view('properties.index',compact('properties','property_type'));
最近它对我有用,但从前一刻到下一刻它都无法识别我的关系,我在索引中完成了 dd 并且关系为空
【问题讨论】:
【参考方案1】:尝试将关系更改为 this。
public function space()
return $this->hasOne('App\Space', 'property_id');
【讨论】:
以上是关于试图获得非对象 Laravel 的属性“浴室”的主要内容,如果未能解决你的问题,请参考以下文章