我想创建一个模型:FormsUser和迁移:forms_user。
为了使用该表作为枢轴表,因为我在创建表单时遇到问题,我被告知表“forms_users不存在”,因为我单独创建了迁移。当我转到我的小组成员管理页面时,我会收到错误:“forms_users不存在”,因为我必须使用一个枢轴表,所以它必须具有这个表名...
你能帮我解决这个问题或提供替代方案吗? 简而言之,我想创建一个表单,并创建一个团队,可以向其中添加其他用户以填写相同的表单。
Migration: forms && Models Forms:
为了使用该表作为枢轴表,因为我在创建表单时遇到问题,我被告知表“forms_users不存在”,因为我单独创建了迁移。当我转到我的小组成员管理页面时,我会收到错误:“forms_users不存在”,因为我必须使用一个枢轴表,所以它必须具有这个表名...
你能帮我解决这个问题或提供替代方案吗? 简而言之,我想创建一个表单,并创建一个团队,可以向其中添加其他用户以填写相同的表单。
Migration: forms && Models Forms:
class Forms extends Model
{
use HasFactory;
use SoftDeletes;
protected $fillable = [
'title',
'description',
'date',
'color',
'progress',
'user_id',
'groupe_id',
'formulaire',
'logo',
];
protected $dates = [
'created_at',
'deleted_at',
'started_at',
'update_at'
];
public function user()
{
return $this->belongsTo(User::class);
}
public function usersGroupe()
{
return $this->belongsToMany(User::class);
}
}
迁移:用户和模型用户:
class User extends Authenticatable
{
use HasApiTokens;
use HasFactory;
use HasProfilePhoto;
use Notifiable;
use TwoFactorAuthenticatable;
use SoftDeletes;
/**
* The attributes that are mass assignable.
*
* @var string[]
*/
protected $fillable = [
'lastname',
'firstname',
'email',
'password',
'current_team_id',
'profile_photo_path',
'role_id',
];
/**
* The attributes that should be hidden for serialization.
*
* @var array
*/
protected $hidden = [
'password',
'remember_token',
'two_factor_recovery_codes',
'two_factor_secret',
];
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
/**
* The accessors to append to the model's array form.
*
* @var array
*/
protected $appends = [
'profile_photo_url',
'role_id',
];
protected $dates = [
'created_at',
'deleted_at',
'started_at',
'update_at'
];
public function forms()
{
return $this->hasMany(Forms::class);
}
public function formGroupe()
{
return $this->belongsToMany(Forms::class);
}
public function role()
{
//return $this->hasMany(Role::class, 'id', 'role_id');
return $this->hasOne(Role::class, 'id', 'role_id');
}
public function user()
{
return $this->hasOne(User::class);
}
}
迁移:formuser && Models FormsUser:
class FormsUser extends Model
{
use HasFactory;
protected $fillable = [
'forms_id',
'user_id'
];
}
迁移 create_forms_user:
Schema::create('forms_user', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->unsignedBigInteger('forms_id');
$table->foreign('forms_id')->references('id')->on('forms');
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');
});