如何使用laravel雄辩的方法联接两个或多个表?

时间:2019-02-11 13:25:29

标签: laravel eloquent eloquent--relationship

对于雄辩的关系联接几乎没有什么困惑,因为到目前为止,我以前都是通过查询生成器来获得结果的。仍然提到其他相关问题,我不清楚。请用一个更好的例子向我解释。

  • 模型1-客户
  • 模型2-customer_items(与customer_id和item_id相关)
  • 模型3-物品(有关物品的详细信息)

现在,我要列出与客户相关的商品详细信息。

将customer_items与其中customer.id = customer_items.user_id和items.id = customer_items.item_id的项目一起加入。

3 个答案:

答案 0 :(得分:1)

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

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

这只是一个小例子。 Stackoverflow不是一个教育性网站,我强烈建议您访问Laravel Relationship Docs。在那您可以学到更多,他们在Laracast上有一个非常好的关于关系的系列文章(如果您是视觉学习者)https://laracasts.com/series/eloquent-relationships

答案 1 :(得分:1)

首先在模型中定义方法。

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

}

希望有帮助。谢谢。

答案 2 :(得分:0)

如果我很好地回答了您的问题,那么您正在寻找查询以获取商品详细信息。

$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');