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

时间:2018-11-07 03:15:59

标签: laravel laravel-5.7

我在2表到3rd表之间建立关系。我总共有3张桌子。

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

2。)人员

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

3。)员工地址

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

现在,我想获取任何人员输入的所有地址的列表。我尝试了以下代码,但返回的地址与Staff 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}] 

Better view

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

关于, 哈桑·拉贾尼。

2 个答案:

答案 0 :(得分:4)

此解决方案非常完美@Capt。蒂莫但是,如果您想为某个工作人员提供地址,那么我在这里提供了解决方案。

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

public function addresses() { return $this->belongsToMany('App\addresses'); }

查询以获取$ staffId的所有地址(其中$ staffId是职员表的ID)

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

此外,如果要在数据透视表中获取其他属性,则在定义关系时必须指定它们。

public function addresses() { return $this->belongsToMany('App\addresses')->withPivot('date_address_from', 'date_address_to'); }

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

以供参考:https://laravel.com/docs/5.7/eloquent-relationships#many-to-many

答案 1 :(得分:0)

首先,尝试修正您的命名约定。如果遵循laravel的命名约定,事情会容易得多。 Here is the guide

  • 数据透视表应为按字母顺序排列的单一模型名称(将staff_addresses更改为address_staff

然后使用belongsToMany关系。

员工模型:

public function addresses()
{
    return $this->belongsToMany('App\addresses');
}

查询方式如下:

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