如何使用laravel eloquent 方法连接两个或多个table?

How to join two or more table using laravel eloquent method?

对 eloquent 关系连接有点困惑,因为到目前为止我过去常常通过查询生成器获取结果。还提到了其他相关问题,我不清楚。请用一个更好的例子来解释我。

现在我想列出客户相关的商品详情。

加入 customer_items 与 customer.id = customer_items.user_id 和 items.id = customer_items.item_id.[= 的项目13=]

首先,您需要这样定义模型:

class Customer extends Model
{
    protected $table = 'customers';

    public function items(){
        return $this->hasMany(Item::class, 'customer_id');
    }

}

class CustomerItem extends Model
{
    protected $table = 'customer_items';

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

}

那么你可以这样称呼关系:

$customer = Customer::find(1); // This will get the first customer in the DB
$itemsOfCostumer = $customer->items // This will return all the items of the customer

// Now let suppose we have an ItemCustomer and we would like to know the owner
$customerItem = CustomerItem::find(1); // Get the first item of a customer in DB
$customer = $customerItem->customer; // Ther you have the customer

这只是一个小例子。 Whosebug 不是教育网站,我强烈建议您访问 Laravel Relationship Docs. Over there you can learn much more and they have a really good series at Laracast about relationships (if you are visual learner) https://laracasts.com/series/eloquent-relationships

如果我解决了你的问题,你正在寻找一个查询来获取项目的详细信息。

$item_details = Items::
    join('customer_items', 'customer_items.item_id', '=', 'items.id')
    ->join('customer', 'customer.id' '=', 'customer_items.customer_id');

或者你可以得到相同的结果:

$item_details = DB::table('items')
    ->join('customer_items', 'customer_items.item_id', '=', 'items.id')
    ->join('customer', 'customer.id' '=', 'customer_items.customer_id');

首先在模型中定义方法。

Customer.php // Customer model

class Customer extends Model
{
    protected $table = 'customers';
    protected $primaryKey = 'customer_id';

    public function customerItems(){

    //customer_id is a foreign key in customer_items table

    return $this->hasMany(Item::class, 'customer_id');

    // A customer will has many items thats why hasMany relation is used here
     }

    }

CustomerItem.php // CustomerItem

class CustomerItem extends Model
{
    protected $table = 'customer_items';
    protected $primaryKey = 'customer_item_id';

    public function itemDetail(){

     //customer_id is a foreign key in customer_items table

     return $this->hasOne(Customer::class, 'customer_item_id');

                //A Item will has single detail thats why hasOne relation used here
     }

    }

在CustomerController.php

use \Customer                  // define customer model path 

public function getCustomerItem(Request $request)
{
    // Eloquent query to get data
    $customer_item_detail_data = Customer::with('customerItems.itemDetail')->get();
    //it return all items of customers with item details 
    //$customer_item_detail_data = Customer::with('customerItems')->with('customerItems.itemDetail')->get(); you can also use in this way

}

希望对您有所帮助。谢谢。