Search code examples
laraveleloquentdefaultfactory

How to change the default Laravel Eloquent table-id-name


When i create a Laravel Factory like this:

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

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

});

, Laravel will require the user_id as a part of the useroption-table, because the origin table is user and the required value is id - ùser_id`.

Now, when i want to use uid instead of user_id, what should i do?


Solution

  • You can set the foreign key the relationship uses like so

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