简体   繁体   English

使用 eloquent laravel 获取连接表数据

[英]fetch join table data using eloquent laravel

I am new to laravel & want to implement eloquent relationship.我是 laravel 的新手,想要实现 eloquent 关系。

Let me explain.让我解释。

Consider I have 2 tables考虑我有 2 张桌子

products产品

 product_id
 product_name
 brand_id
 price

brands品牌

 id
 brand_name

Each product will have one brand Id.But in Brands table, there is no product id.每个产品都有一个品牌 ID。但在 Brands 表中,没有产品 ID。 One brand_id can be in multiple product rows, and one product has one brand_id only.一个brand_id 可以在多个产品行中,一个产品只有一个brand_id。 I want to select some col from products table plus brand_name with respect to brand_id of products table using Model.SO in Product model I wrote:我想使用 Model.SO 在产品 model 中使用 Model.SO 从产品表加上品牌名称对产品表的品牌 ID 的一些列进行 select 我写道:

public function brands()
    {   
        
        return $this->hasOne('App\Brand','product_id');
    }

and in Brand model I write:在 model 品牌中,我写道:

public function products()
    {
        return $this->belongsTo('App\Product','brand_id');
    } 

Now I want the result:现在我想要结果:

product_name
price
brand_name

How can I fetch those data in controller using eloquent relation?如何使用 eloquent 关系在 controller 中获取这些数据? Also, the way I wrote Model relationship, Is it ok??还有,我写Model关系的方式,可以吗??

Your Product Model relation will be below您的产品 Model关系如下

public function brand(){   
    return $this->belongsTo('App\Brand','brand_id');
}
public function product(){   
    return $this->belongsTo('App\Product','product_id');
}

Now in controller you can add query as below.现在在 controller 中,您可以添加如下查询。

$products = Product::with('brand','product')->get();
echo '<pre>'
print_r($products->toArray());
exit;

It looks to me like you want a one-to-many relationship, so one brand can have many products and many products belong to one brand.在我看来,你想要一个one-to-many关系,所以一个品牌可以有很多产品,很多产品属于一个品牌。

Product model:产品 model:

public function brand()
{
    return $this->belongsTo('App\Brand');
}

Brand model:品牌 model:

public function products()
{
    return $this->hasMany('App\Product');
}

Then you would be able to get the brand information like this:然后你就可以得到这样的品牌信息:

The full brand model:全品牌model:

Product::first()->brand

The brand name:品牌名称:

Product::first()->brand->brand_name

From thedocs :文档

A one-to-many relationship is used to define relationships where a single model owns any amount of other models.一对多关系用于定义单个 model 拥有任意数量的其他模型的关系。 For example, a blog post may have an infinite number of comments.例如,一篇博文可能有无数条评论。

PS: PS:

Your table column names do not make much sense to me, why do you have product_id on products but then on brands it is just called id ?您的表列名称对我来说没有多大意义,为什么您在 products 上有product_id但在品牌上它只是被称为id Why do you have product_name but then just price ?为什么你有product_name但只有price why is it not just name or product_price , so your at least consistent?为什么不只是nameproduct_price ,所以你至少是一致的?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM