问题描述
我使用全局范围将我的用户模型划分为学生和教师。我的意思是我的学生和教师模型扩展了用户模型。 另外,我使用 UUID 而不是增量 ID。
这是我的用户模型类:
class User extends Authenticatable
{
use HasApiTokens,HasFactory,Notifiable,HasRoles;
protected $guard_name = 'web';
protected $fillable = [
'firstname','lastname','sex','mobile','password',];
protected $hidden = [
'password','remember_token','mobile_verified_at','updated_at','created_at'
];
protected $casts = [
'mobile_verified_at' => 'datetime',];
public function getRouteKeyName()
{
return 'id';
}
public function getIncrementing()
{
return false;
}
public function getKeyType()
{
return 'string';
}
public function academy()
{
return $this->belongsToMany(Academy::class);
}
}
这是我的学生模型课:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
use Spatie\Permission\Traits\HasPermissions;
class Student extends User
{
protected static function booted()
{
static::addGlobalScope('students',function (Builder $builder) {
$builder->role('student')->join('users',function($join){
$join->on('students.id','=','users.id');
});
});
}
}
我的问题:
当我尝试使用 Student::firstOrCreate()
时,我会遇到此错误,例如:
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous (SQL: select * from `students` inner join `users` on `students`.`id` = `users`.`id` where (`id` = cd7873e5-f4e8-47dd-8d19-4b7bb73a90b3) and exists (select * from `roles` inner join `model_has_roles` on `roles`.`id` = `model_has_roles`.`role_id` where `students`.`id` = `model_has_roles`.`model_id` and `model_has_roles`.`model_type` = App\\Models\\Student and `roles`.`id` in (3d1e9c31-c7dd-4373-927c-9a2b4accca64)) limit 1)
而且 Student::Create()
没有问题!!
那么我该如何解决这个问题?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)