Laravel 拉威尔雄辩关系模型

Laravel 拉威尔雄辩关系模型,laravel,eloquent,relationship,Laravel,Eloquent,Relationship,我的数据库中有3个表: table 1 = user (hasMany order) table 2 = order (hasMany order_detail, belongsTo user) table 3 = order_detail (belongsTo order) 在我的订单详情模型中,我添加了以下功能: public function order() { return $this->belongsTo('App\Order'); } 因此,我可以调用订单数据,而无

我的数据库中有3个表:

table 1 = user (hasMany order)
table 2 = order (hasMany order_detail, belongsTo user)
table 3 = order_detail (belongsTo order)
在我的订单详情模型中,我添加了以下功能:

public function order() {
    return $this->belongsTo('App\Order');
}
因此,我可以调用订单数据,而无需从控制器中定义它,我只需在控制器上定义订单详细信息

$order_detail->order->invoice_number
但是如何从订单细节调用用户数据呢

我试着用这个

$order_detail->order->user
但这对我没用


有没有办法调用父关系?

在订单模型中为订单详细信息添加函数:

public function order_details() {
    return $this->hasMany('order_detail');
}
对于用户:

public function user() {
    return $this->belongsTo('user');
}
在用户模型中添加:

public function orders() {
    return $this->hasMany('order');
}
然后,您可以调用控制器:

$details = Order::find(1)->order_details->where('order_detail_id', 5)->first();
$userName = $details->username;
            //           ^
            // column name in order_detail table

有关详细信息,请参见。

订单模型中的“添加订单详细信息”功能:

public function order_details() {
    return $this->hasMany('order_detail');
}
对于用户:

public function user() {
    return $this->belongsTo('user');
}
在用户模型中添加:

public function orders() {
    return $this->hasMany('order');
}
然后,您可以调用控制器:

$details = Order::find(1)->order_details->where('order_detail_id', 5)->first();
$userName = $details->username;
            //           ^
            // column name in order_detail table

更多信息请参见。

我认为这是定义关系的更完整方法:

// User model

public function orders(){

    // I'm telling that users has many order. The orders have an user_id field that match with the ID field of the user.
    return $this->hasMany('App/Order', 'user_id' , 'id');

}


// Order model

public function order_details(){

    // I'm telling that order has many order_details. The order_details have an order_id field that match with the ID field of the order.
    return $this->hasMany('App/OrderDetail', 'order_id' , 'id');

}

public function user(){

    // I'm telling that an order belongs to an user. The user has an ID field that match with the order_id field of the order.
    return $this->belongTo('App/User', 'id', 'user_id');
}


// OrderDetail Model

public function order(){

    // I'm telling that an order detail belongs to an order. The order  has an ID field that match with the order_id field of the order detail.
    return $this->belongTo('App/Order', 'id', 'order_id');
}

我已经看到,您只将模型名称作为关系定义中的第一个参数。我认为您必须设置从根到模型的相对路径。在我的例子中,我将模型作为应用程序文件夹的子对象。

我认为这是定义关系的更完整的方法:

// User model

public function orders(){

    // I'm telling that users has many order. The orders have an user_id field that match with the ID field of the user.
    return $this->hasMany('App/Order', 'user_id' , 'id');

}


// Order model

public function order_details(){

    // I'm telling that order has many order_details. The order_details have an order_id field that match with the ID field of the order.
    return $this->hasMany('App/OrderDetail', 'order_id' , 'id');

}

public function user(){

    // I'm telling that an order belongs to an user. The user has an ID field that match with the order_id field of the order.
    return $this->belongTo('App/User', 'id', 'user_id');
}


// OrderDetail Model

public function order(){

    // I'm telling that an order detail belongs to an order. The order  has an ID field that match with the order_id field of the order detail.
    return $this->belongTo('App/Order', 'id', 'order_id');
}

我已经看到,您只将模型名称作为关系定义中的第一个参数。我认为您必须设置从根到模型的相对路径。在我的例子中,我将模型作为应用程序文件夹的孩子。

例如,如何从id为5的订单详细信息中获取用户名?我更新了答案,但我不知道“id”是什么意思。是订单号吗?您可以在where子句中更改列名。例如,如何从id为5的订单详细信息中获取用户名?我更新了答案,但我不知道“id”是什么意思。是订单号吗?您可以在where子句中更改列名。我想您可以查看以下帖子:@luigonsec我已经成功创建了2个表关系,但现在要创建3个表$订单\详细信息->订单->用户->第一名您发布的内容应该有效。你可以发布关于如何设置关系的代码(而不是psueo代码)吗?我想你可以查看以下帖子:@luigonsec我已经成功创建了2个表关系,但现在要创建3个表$订单\详细信息->订单->用户->第一名您发布的内容应该有效。您能否发布关于如何设置关系的代码(而不是psueo代码)?