【问题标题】:How to join two or more table using laravel eloquent method?如何使用 laravel eloquent 方法连接两个或多个表?
【发布时间】:2019-07-05 00:12:48
【问题描述】:

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

【问题讨论】:

标签: laravel eloquent eloquent-relationship


【解决方案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】:

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

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

    【讨论】:

    • 是的,但是你能不能用雄辩的关系方法给一个sn-p?
    【解决方案3】:

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

    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

    【讨论】:

      猜你喜欢
      • 2016-07-22
      • 2019-06-28
      • 1970-01-01
      • 2015-09-06
      • 2014-10-11
      • 2019-10-04
      • 1970-01-01
      • 2015-07-12
      • 1970-01-01
      相关资源
      最近更新 更多