Laravel:使用Eloquent belongssto,从两个表中获取数据?

时间:2022-10-16 07:37:51

In Controller, I would like to pass the only one variable with specifies column from parent in it. Now,I'm using

在Controller中,我想传递唯一一个变量,其中包含来自parent的指定列。现在,我正在使用

View::make('store.product')->with('products', Product::find($id)
->join('design','product.design_id','=','design.id')
->join('user','design.user_id','=','user.id')
->select('user.name','design.title','product.price')
->get();

My question is

我的问题是

1.Is there a better way to do this by using Belongsto?

1.使用Belongsto有更好的方法吗?

2.If can do, Is it work the same with Hasmany?

2.如果可以的话,它与Hasmany一样吗?

This is my table structure.

这是我的表结构。

User

id | name
1  | 'John'

Design

id | user_id |  title
1  |    1    | 'Chill'  
2  |    1    |  'Mad'

Product

id | design_id | price
1  |     1     |  20  
2  |     1     |  30

And Model be like this

而模型就像这样

Product belongsto Design , Design belongsto User

产品属于设计,设计属于用户

1 个解决方案

#1


6  

Add a method to your Users like so for your designs that the user has;

为您的用户添加一个方法,就像用户所拥有的设计一样;

public function designs(){
    $this->hasMany('Design');
}

For the designs model add the following methods;

对于设计模型,添加以下方法;

public function user(){
    $this->belongsTo('User');
}

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

For your products model

为您的产品型号

public function design(){
    $this->belongsTo('Design');
}

These will set up the relationship allowing you to eager load the data on your models.

这些将建立关系,允许您急切加载模型上的数据。

This can be done like so;

这可以这样做;

$variable = Product::with('designs')->find(1); //This will load the product with the related designs

If you want all the designs and the users that belong to the designs do the following;

如果您希望所有设计和属于设计的用户执行以下操作;

$variable = Product::with('designs', 'design.user')->find(1); //This will load the designs that relate to the Product and include the user that that design belongs to on the Design model.

To access the properties use the following;

要访问这些属性,请使用以下内容:

$variable->designs //This will return a collection of all the designs.
$variable->designs->first()->user //This will return the user model that the design belongs to.

An example of displaying the information;

显示信息的一个例子;

@foreach ($variable->designs as $design)
    {{ $design->user->username }}
@endforeach

Please note: i have not tested this code.

请注意:我还没有测试过这段代码。

#1


6  

Add a method to your Users like so for your designs that the user has;

为您的用户添加一个方法,就像用户所拥有的设计一样;

public function designs(){
    $this->hasMany('Design');
}

For the designs model add the following methods;

对于设计模型,添加以下方法;

public function user(){
    $this->belongsTo('User');
}

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

For your products model

为您的产品型号

public function design(){
    $this->belongsTo('Design');
}

These will set up the relationship allowing you to eager load the data on your models.

这些将建立关系,允许您急切加载模型上的数据。

This can be done like so;

这可以这样做;

$variable = Product::with('designs')->find(1); //This will load the product with the related designs

If you want all the designs and the users that belong to the designs do the following;

如果您希望所有设计和属于设计的用户执行以下操作;

$variable = Product::with('designs', 'design.user')->find(1); //This will load the designs that relate to the Product and include the user that that design belongs to on the Design model.

To access the properties use the following;

要访问这些属性,请使用以下内容:

$variable->designs //This will return a collection of all the designs.
$variable->designs->first()->user //This will return the user model that the design belongs to.

An example of displaying the information;

显示信息的一个例子;

@foreach ($variable->designs as $design)
    {{ $design->user->username }}
@endforeach

Please note: i have not tested this code.

请注意:我还没有测试过这段代码。