簡體   English   中英

Laravel 5雄辯地從第二個聯接表的外鍵包含另一個表的行

[英]Laravel 5 eloquent include the row of the other table from the foreign key of the 2nd joined table

我有3個樣本模型

  1. 顧客

    ID

  2. 訂購

    ID

    顧客ID

    deliver_address_id

  3. customer_address

    id客戶ID

    國家

客戶可以在其個人資料上保存許多地址,但每個訂單只能選擇一個地址。

我想使用laravel 5雄辯地獲得所有客戶訂單,包括一次查詢中的地址。 我是laravel的新手,因此難以查詢復雜的關系表。

class Customer extends Model {

    $table = 'customers';

    function addresses(){
        return $this->hasMany(Address::class, 'customer_id');
    }

    function orders(){
        return $this->hasMany(Order::class, 'customer_id');
    }

}

class Order extends Model {

    $table = 'orders';

    function address(){
        return $this->belongsTo(Address::class, 'address_id');
    }

    function customer() {
        return $this->belongsTo(Customer::class, 'customer_id');
    }
}

class Address extends Model{

    $table = 'customer_addresses';

    function customer(){
        return $this->belongsTo(Customer::class, 'customer_id');
    }

}


TABLES : 
1 customers

        id

2 orders

        id

        customer_id

        address_id

3 customer_addresses

        id

        customer_id

        street

        country

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM