【问题标题】:How to change the default Laravel Eloquent table-id-name如何更改默认 Laravel Eloquent table-id-name
【发布时间】:2020-01-18 11:08:20
【问题描述】:

当我像这样创建 Laravel 工厂时:

factory(App\User::class, 10)->create()->each(function ($user) {

    $user->useroptions()->save(factory(App\UserOption::class)->make(['user_id' => $user->id]));

});

,Laravel 将需要 user_id 作为 useroption-table 的一部分,因为源表是 user 并且所需的值是 id -ùser_id`。

现在,当我想使用uid 而不是user_id 时,我应该怎么做?

【问题讨论】:

  • 在定义关系的时候需要定义自定义的primary id

标签: laravel eloquent default factory


【解决方案1】:

您可以像这样设置关系使用的外键

public function useroptions() {
    return $this->hasMany('App\UserOption', 'uid');
}

【讨论】:

    猜你喜欢
    • 2020-02-28
    • 1970-01-01
    • 2013-11-30
    • 2022-12-11
    • 2020-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多