访问laravel 5中的嵌套关系

访问laravel 5中的嵌套关系,laravel,laravel-5,eloquent,relation,Laravel,Laravel 5,Eloquent,Relation,我有如下3种型号: Date Property PropertyDetail 这是我对按顺序编写的表的迁移 日期: 财产: public function up() { Schema::create('properties', function (Blueprint $table) { $table->bigIncrements('id'); $table->integer('type'); $table->text(

我有如下3种型号:

Date
Property
PropertyDetail
这是我对按顺序编写的表的迁移 日期:

财产:

 public function up()
{
    Schema::create('properties', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->integer('type');
        $table->text('title');
        $table->integer('base_capacity');
        $table->timestamps();
    });
和PropertyDetail:

 public function up()
{
    Schema::create('property_details', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->integer('property_id');
        $table->string('state');
        $table->string('city');
        $table->timestamps();
    });
}
我试图从迁移中删除不必要的字段,以保持它的干净,因此这是我的2个日期和项目 日期模型:

 public function properties() {
    return $this->belongsToMany(Property::class);
 }
和属性模型:

 public function dates(){
    return $this->belongsToMany(Date::class);
}
 public function features(){
    return $this->hasMany(Feature::class);
}
好的,现在,在我的控制器中,我要执行以下操作:

 $search = Date::with('properties')
        ->where('date',$someday)->get()


     ->and some how here i want to select the city from the property detail table
;
这就是我的问题,现在我可以很容易地访问属性并显示其名称,但从这个关系中,我如何访问功能关系并从中选择城市我应该使用连接还是其他什么我希望我已经足够清楚,我想做的是什么

您可以这样做:

属性模型:

public function details(){
    return $this->belongsToMany(Details::class,'property_details','id','property_id');
}
 public function properties() {
     return $this->belongsToMany(Property::class);
 }

 public function propertiesDetails() {
     return $this->properties()->with('details');
 }
在日期模型中:

public function details(){
    return $this->belongsToMany(Details::class,'property_details','id','property_id');
}
 public function properties() {
     return $this->belongsToMany(Property::class);
 }

 public function propertiesDetails() {
     return $this->properties()->with('details');
 }
在您的控制器中您可以通过使用获得属性的详细信息:

 $search = Date::with('propertiesDetails')
    ->where('date',$someday)->get();
现在您可以访问属性的详细信息


希望有帮助。

您在哪里使用此代码
public function properties(){return$this->belongtomany(Property::class);}
我在日期模型中使用的是,根据嵌套关系??现在您可以访问城市、州,。。。在你的财产里。这就是我认为你需要的。不是吗@Farshadfb抱歉兄弟,但不,如果我不清楚,我很抱歉,我想知道如何访问与其他2个相关表的第三个关系Farshadfb在这里