0

对 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 的项目连接起来。

4

3 回答 3

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 关系文档。在那里你可以学到更多,他们在 Laracast 有一个关于关系的非常好的系列(如果你是视觉学习者)https://laracasts.com/series/eloquent-relationships

于 2019-02-11T13:44:42.263 回答
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

}

希望能帮助到你。谢谢你。

于 2019-02-12T04:24:31.953 回答
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');
于 2019-02-11T14:07:32.843 回答