php - 两个相同模型/表之间的两种关系

标签 php mysql laravel foreign-keys laravel-5.5

问题

您好,我在尝试处理两个模型(Laravel 5.5)之间的两种关系时遇到了一些麻烦:OrderUser

1-----米 |一个用户(作为客户)可以发出多个订单
1-----米 |一个用户(作为员工)可以接受多个订单

因此,在我的 orders 表中,我将 user_idemployee_id 作为外键。

现在,某些订单没有初始 employee_id,因为需要接受订单,然后我设置该值。

使用tinker,我找到了这个Order对象(id:30),它有一个user_id和一个employee_id但是当我dd() 它,返回:

=> App\Models\Order {#882
     order_id: 30,
     user_id: 7,  // <-- foreign key
     employee_id: 6,  // <-- foreign key
     restaurant_id: 4,  // <-- foreign key (for other table)
     // ...
     total: "120.00",
     created_at: "2018-01-31 00:15:41",
     updated_at: "2018-01-31 00:15:41",
     deleted_at: null,
     restaurant: App\Models\Restaurant {#900 // <- MY RESTAURANT RELATION (M-1)
       restaurant_id: 4,
       name: "Stark, Padberg and Buckridge",
       // ...
       // ...
       created_at: "2018-01-12 18:18:17",
       updated_at: "2018-01-12 18:18:17",
       deleted_at: null,
     },
     customer: App\Models\User {#914 // <- MY CUSTOMER RELATION (M-1)
       user_id: 7,
       restaurant_id: 4,
       // ...
       created_at: "2018-01-30 17:52:33",
       updated_at: "2018-01-30 23:32:00",
       deleted_at: null,
     },
     deliverer: null,  // <-- THIS SHOULD CONTAIN THE EMPLOYEE DATA
//                            THAT BELONGS TO THE DELIVERER RELATION
     }

我做错了什么?

-----

我的逻辑信息。

型号

订单

在我的 Order 模型中,我定义了关系(以及其他关系)如下(我的所有主键都遵循此约定:model_id):

/**
 * Return the user who created the order.
 *
 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
 */
public function customer()
{
    return $this->belongsTo(
        'App\Models\User',
        'user_id',
        'user_id'
    );

}

/**
 * Return the employee (user) who deliver the order.
 *
 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
 */
public function deliverer()
{
    return $this->belongsTo(
        'App\Models\User',
        'employee_id',
        'user_id'
    );
}

用户

/**
 * A customer can make many orders
 * 
 * @return \Illuminate\Database\Eloquent\Relations\HasMany
 */
public function ordersMade()
{
    return $this->hasMany('App\Models\Order',
        'user_id',
        'user_id'
    );
}

/**
 * An employee attends many orders.
 *
 * @return \Illuminate\Database\Eloquent\Relations\HasMany
 */
public function ordersDelivered()
{
    return $this->hasMany('App\Models\Order',
        'employee_id',
        'user_id'
    );
}

表格

这是我的orders 表的架构:

orders table

这是我的users 表的架构:

users table

<小时/>

更新

是的,我有 ID 为 67 的用户:

Table

在 Tinker 中我做了:

>>> $o = App\Models\Order::find(30);
>>> $o->customer;
>>> $o->restaurant;
>>> $o->deliverer;
>>> $o;

最佳答案

嗯,我的代码没有任何问题。我在模型上将 softDeletes 设置为 true,因此我试图获取“已删除”元素的属性。

就这么简单。感谢@MahdiYounesi,他让我检查了这一点。

虽然这个问题几乎是一个愚蠢的问题,但值得注意的是,当您在从实体查询时还需要返回 softDeleted 记录时,您可以 -as docs说 - 在需要此类结果的每个查询中添加 withTrashed() ,或者在定义关系时直接在模型中添加,如下所示:

/**
 * Return the user who created the order.
 *
 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
 */
public function customer()
{
    return $this->belongsTo(
        'App\Models\User',
        'user_id'
    )->withTrashed();

}

/**
 * Return the employee (user) who deliver the order.
 *
 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
 */
public function deliverer()
{
    return $this->belongsTo(
        'App\Models\User',
        'employee_id',
        'user_id'
    )->withTrashed();
}

这将收集“已删除”的数据,例如对于历史信息很有用。

关于php - 两个相同模型/表之间的两种关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48549606/

相关文章:

Laravel 包 : Route:resource() don't pass the parameters

php - 如何测试浏览器以查看它是否支持 128 位加密?

php - Laravel Eloquent,如何处理 UNIQUE 错误?

sql - MySQL 限制每个类别的结果

java - 将 HTML 输入类型 DATE 转换为 MYSQL DATE

php - 从 MySQL 中检索多行

php - Laravel 中的多态关系问题

javascript - .htaccess:使用 RewriteCond 但对 IMAGES/JS/CSS 文件的 href 仍然被不必要地重写

php - Laravel关系空外键获取

php - 拉维尔 5.4 : The only supported ciphers are AES-128-CBC and AES-256-CBC with the correct key lengths