Красноречивый, где ищет 'user_id' вместо 'id', хотя я определил 'id' - PullRequest
0 голосов
/ 07 декабря 2018

У меня есть таблица пользователей:

Schema::create('users', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->string('email')->unique();
    $table->integer('role_id')->unsigned();
    $table->timestamp('email_verified_at')->nullable();
    $table->string('password');
    $table->rememberToken();
    $table->timestamps();
});

Таблица ролей:

Schema::create('roles', function (Blueprint $table) {
    $table->increments('id');
    $table->string('role');
    $table->timestamps();
});

И таблица платежей:

Schema::create('payments', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('user_id')->unsigned();
    $table->integer('driver_id')->unsigned();
    $table->integer('amount');
    $table->date('payment_date');
    $table->timestamps();
});

Я также добавил внешние ограничениядля пользователей по ролям (role_id).

Schema::table('users', function ($table) {
            $table
            ->foreign('role_id')
            ->references('id')
            ->on('roles')
            ->onUpdate('cascade');
        });

Когда я пытался генерировать фиктивные платежи с помощью фейкера, я пытаюсь получить случайный user_id из users, где его role равно user,и другое случайное значение user_id из users, где его role равно driver.

$factory->define(App\Models\Payment::class, function (Faker $faker) {
    return [
        'user_id' => App\User:: //get user
                        whereHas('roles', function ($query) {
                            $query->where('role', 'user');}) //where its role is user
                        ->select('id') //get its id
                        ->get()
                        ->random(),
        'driver_id' => App\User::
                        whereHas('roles', function ($query) {
                            $query->where('role', 'driver');})
                        ->select('id')
                        ->get()
                        ->random(),
        'amount' => $faker->randomNumber,
        'payment_date' => $faker->date($format = 'Y-m-d', $max = 'now')
    ];
});

Однако с whereHas выдает ошибку:

Illuminate\Database\QueryException  : SQLSTATE[42S22]: Column not found: 1054 Unknown column 'roles.user_id' in 'where clause' (SQL: select `id` from `users` where exists (select * from `roles` where `users`.`id` = `roles`.`user_id` and `role` = driver))

Если я удаляюwhereHas часть, подобная этой, она работает:

App\User::select('id')
           ->get()
           ->random(),

Я уверен, что в моем запросе whereHas я не искал user_id в roles таблице, так почему он пытается это сделать?

РЕДАКТИРОВАТЬ: вот моя модель

роль

class Role extends Model
{
    protected $table = 'roles';
    protected $fillable = [
        'role'
    ];

    public function user()
    {
        return $this->hasMany('App\User');
    }
}

пользователь

class User extends Authenticatable
{
    use Notifiable;

    protected $fillable = [
        'name', 'email', 'password', 'role_id'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    public function payments()
    {
        return $this->hasMany('App\Models\Payment');
    }

    public function roles()
    {
        return $this->belongsTo('App\Models\Role');
    }
}

1 Ответ

0 голосов
/ 07 декабря 2018

Вот решение, которое я нашел.Laravel предполагает, что внешний ключ для roles в пользовательской модели равен roles_id, хотя я использовал role_id.Чтобы переопределить это, поместите другой параметр role_id после модели в функцию belongsTo().в модели пользователя

public function roles()
{
    return $this->belongsTo('App\Models\Role','role_id');
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...