Saya membangun hubungan antara 2 tabel hingga tabel ke-3. Saya memiliki total 3 meja.
1.) alamat
Schema::create('addresses', function (Blueprint $table) {
$table->increments('id');
$table->text('line_1_number_building')->nullable();
$table->text('lineSchema::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();
});
number_street')->nullable();
$table->text('lineSchema::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();
});
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.) staf
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.) alamat_staf
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();
});
Sekarang saya ingin mendapatkan daftar semua alamat untuk setiap entri staf. Saya mencoba kode di bawah ini tetapi ia mengembalikan alamat yang cocok dengan ID ID Staf
Apa yang saya coba
public function saddresses()
{
return $this->hasManyThrough('App\addresses','App\staff_addresses','staff_id','id');
}
Apa yang saya dapatkan
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}]
Bisakah Anda membimbing saya bagaimana saya bisa membangun hubungan yang benar?
Salam, Hasan Rajani.