Laravel Eloquent-通过其他表连接2个表

Laravel Eloquent-通过其他表连接2个表,laravel,laravel-5.7,Laravel,Laravel 5.7,我在两个表到第三个表之间建立关系。我一共有三张桌子 1.)地址 Schema::create('addresses', function (Blueprint $table) { $table->increments('id'); $table->text('line_1_number_building')->nullable(); $table->text('line_2_number_street')->nullable(); $

我在两个表到第三个表之间建立关系。我一共有三张桌子

1.)地址

Schema::create('addresses', function (Blueprint $table) {
    $table->increments('id');
    $table->text('line_1_number_building')->nullable();
    $table->text('line_2_number_street')->nullable();
    $table->text('line_3_area_locality')->nullable();
    $table->text('city')->nullable();
    $table->text('zip_code')->nullable();
    $table->text('state_province_county')->nullable();
    $table->text('country')->nullable();
    $table->longText('other_address_details')->nullable();
    $table->timestamps();
});
Schema::create('staffs', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('staff_category_id');
    $table->integer('gender_id');
    $table->text('staff_job_title')->nullable();
    $table->text('staff_first_name')->nullable();
    $table->text('staff_middle_name')->nullable();
    $table->text('staff_last_name')->nullable();
    $table->text('staff_qualifications')->nullable();
    $table->date('staff_birth_date');
    $table->longText('other_staff_details')->nullable();
    $table->timestamps();
});
Schema::create('staff_addresses', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('staff_id');
    $table->integer('address_id');
    $table->date('date_address_from')->nullable();
    $table->date('date_address_to')->nullable();
    $table->timestamps();
});
2.)员工

Schema::create('addresses', function (Blueprint $table) {
    $table->increments('id');
    $table->text('line_1_number_building')->nullable();
    $table->text('line_2_number_street')->nullable();
    $table->text('line_3_area_locality')->nullable();
    $table->text('city')->nullable();
    $table->text('zip_code')->nullable();
    $table->text('state_province_county')->nullable();
    $table->text('country')->nullable();
    $table->longText('other_address_details')->nullable();
    $table->timestamps();
});
Schema::create('staffs', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('staff_category_id');
    $table->integer('gender_id');
    $table->text('staff_job_title')->nullable();
    $table->text('staff_first_name')->nullable();
    $table->text('staff_middle_name')->nullable();
    $table->text('staff_last_name')->nullable();
    $table->text('staff_qualifications')->nullable();
    $table->date('staff_birth_date');
    $table->longText('other_staff_details')->nullable();
    $table->timestamps();
});
Schema::create('staff_addresses', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('staff_id');
    $table->integer('address_id');
    $table->date('date_address_from')->nullable();
    $table->date('date_address_to')->nullable();
    $table->timestamps();
});
3.)员工地址

Schema::create('addresses', function (Blueprint $table) {
    $table->increments('id');
    $table->text('line_1_number_building')->nullable();
    $table->text('line_2_number_street')->nullable();
    $table->text('line_3_area_locality')->nullable();
    $table->text('city')->nullable();
    $table->text('zip_code')->nullable();
    $table->text('state_province_county')->nullable();
    $table->text('country')->nullable();
    $table->longText('other_address_details')->nullable();
    $table->timestamps();
});
Schema::create('staffs', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('staff_category_id');
    $table->integer('gender_id');
    $table->text('staff_job_title')->nullable();
    $table->text('staff_first_name')->nullable();
    $table->text('staff_middle_name')->nullable();
    $table->text('staff_last_name')->nullable();
    $table->text('staff_qualifications')->nullable();
    $table->date('staff_birth_date');
    $table->longText('other_staff_details')->nullable();
    $table->timestamps();
});
Schema::create('staff_addresses', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('staff_id');
    $table->integer('address_id');
    $table->date('date_address_from')->nullable();
    $table->date('date_address_to')->nullable();
    $table->timestamps();
});
现在,我想获得任何员工条目的所有地址列表。我尝试了下面的代码,但它返回的地址和员工ID的ID匹配

我试过的

public function saddresses()
{
    return $this->hasManyThrough('App\addresses','App\staff_addresses','staff_id','id');
}
我得到了什么

Address : [{"id":3,"line_1_number_building":"Hasan 2 line 1","line_2_number_street":"Hasan 2 line 2","line_3_area_locality":"Hasan 2 line 3","city":"Hasan 2 city","zip_code":"Hasan 2 zip code","state_province_county":"Hasan 2 state","country":"Hasan 2 country","other_address_details":"Hasan 2 other details","created_at":null,"updated_at":null,"staff_id":5},{"id":4,"line_1_number_building":"Sadiq 2 line 1","line_2_number_street":"Sadiq 2 line 2","line_3_area_locality":"Sadiq 2 line 3","city":"Sadiq 2 city","zip_code":"Sadiq 2 zip code","state_province_county":"Sadiq 2 state","country":"Sadiq 2 country","other_address_details":"Sadiq 2 other details","created_at":null,"updated_at":null,"staff_id":5}] 

你能指导我如何建立适当的关系吗

问候,,
哈桑·拉贾尼。

首先,尝试修复您的命名约定。如果你遵循拉雷维尔的命名惯例,事情会容易得多

  • 数据透视表应为按字母顺序排列的单数型号名称(将
    员工地址
    更改为
    员工地址
然后使用
belongtomany
关系

员工型号:

public function addresses()
{
    return $this->belongsToMany('App\addresses');
}
以下是您的查询方式:

$data = Staff::with('addresses')->get();

首先,尝试修复命名约定。如果你遵循拉雷维尔的命名惯例,事情会容易得多

  • 数据透视表应为按字母顺序排列的单数型号名称(将
    员工地址
    更改为
    员工地址
然后使用
belongtomany
关系

员工型号:

public function addresses()
{
    return $this->belongsToMany('App\addresses');
}
以下是您的查询方式:

$data = Staff::with('addresses')->get();

这个解决方案非常完美@Teemo船长。但是,如果你想要一个特定员工的地址,那么我在这里提供了解决方案

您必须在员工模型中定义此功能

公共函数地址()
{
返回$this->belongtomany('App\addresses');
}

查询以获取$staffId的所有地址(其中$staffId是staff表的id)

$data=Staff::with('address')->where('id',$staffId)->first()

此外,如果希望在透视表中获得额外属性,则必须在定义关系时指定这些属性

公共函数地址()
{
返回$this->belongtomany('App\addresses')->with pivot('date\u address\u from','date\u address\u to');
}

此外,您还可以使用关系中的
with timestamps
方法自动维护created_at和updated_at列


供参考:

此解决方案非常适合蒂莫船长。但是,如果你想要一个特定员工的地址,那么我在这里提供了解决方案

您必须在员工模型中定义此功能

公共函数地址()
{
返回$this->belongtomany('App\addresses');
}

查询以获取$staffId的所有地址(其中$staffId是staff表的id)

$data=Staff::with('address')->where('id',$staffId)->first()

此外,如果希望在透视表中获得额外属性,则必须在定义关系时指定这些属性

公共函数地址()
{
返回$this->belongtomany('App\addresses')->with pivot('date\u address\u from','date\u address\u to');
}

此外,您还可以使用关系中的
with timestamps
方法自动维护created_at和updated_at列


仅供参考:

向您展示从您的表中有说服力的控制器功能,似乎您可能需要BelongToMany关系。向您展示从您的表中有说服力的控制器功能,似乎您需要BelongToMany关系。感谢Teemo船长指导我命名约定,这对我有很大帮助:)。事实上,我正在学习laravel,但不确定许多功能和规则。对于我的问题,我发现这个解决方案很有帮助()在员工模型上创建了新的函数“addresses”,然后在as arguments==>(return$this->belongsToMany('App\addresses','Staff\u addresses','Staff\u id','address\u id')下添加了透视表名和连接字段名。)如果遵循laravel命名约定,则不需要在关系中指明表名,即
staff\u addresses
。由于您正在加入
地址
员工
表,Laravel会自动搜索
地址
作为轴心。您的参数将是
return$this->belongstomy('App\addresses')(return$this->belongsToMany('App\addresses','Staff\u addresses','Staff\u id','address\u id')下添加了透视表名和连接字段名。)如果遵循laravel命名约定,则不需要在关系中指明表名,即
staff\u addresses
。由于您正在加入
地址
员工
表,Laravel会自动搜索
地址
作为轴心。您的参数将是
return$this->belongstomy('App\addresses')