Laravel如何使用Multi-Guard处理一对一和一对多具有多多态关系的表
我正在开发一个社交网络系统,它有两个守卫(web和admin) webguard用于公共用户,admin guard用于后端用户。还有两种型号用户和管理员型号 系统的功能之一是用户能够创建自己的配置文件。用户模型和配置文件模型之间的关系是一种关系,这些是我的迁移 用户迁移Laravel如何使用Multi-Guard处理一对一和一对多具有多多态关系的表,laravel,eloquent,laravel-8,poly,polymorphic-relationship,Laravel,Eloquent,Laravel 8,Poly,Polymorphic Relationship,我正在开发一个社交网络系统,它有两个守卫(web和admin) webguard用于公共用户,admin guard用于后端用户。还有两种型号用户和管理员型号 系统的功能之一是用户能够创建自己的配置文件。用户模型和配置文件模型之间的关系是一种关系,这些是我的迁移 用户迁移 Schema::create('users', function (Blueprint $table) { $table->id(); $table->string
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->boolean('active')->default(1);
$table->rememberToken();
$table->softDeletes();
$table->timestamps();
});
Schema::create('profiles', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->date('dob')->nullable();
$table->unsignedBigInteger('user_id')->nullable()->unique();
$table->foreign('user_id')->references('id')->on('users')->onDelete('set null')->onUpdate('cascade');
$table->mediumText('address')->nullable();
$table->text('bio')->nullable();
$table->enum('sex', ['male','female'])->default('female');
$table->string('mobile')->nullable();
$table->softDeletes();
$table->timestamps();
});
配置文件迁移
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->boolean('active')->default(1);
$table->rememberToken();
$table->softDeletes();
$table->timestamps();
});
Schema::create('profiles', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->date('dob')->nullable();
$table->unsignedBigInteger('user_id')->nullable()->unique();
$table->foreign('user_id')->references('id')->on('users')->onDelete('set null')->onUpdate('cascade');
$table->mediumText('address')->nullable();
$table->text('bio')->nullable();
$table->enum('sex', ['male','female'])->default('female');
$table->string('mobile')->nullable();
$table->softDeletes();
$table->timestamps();
});
此外,管理员可以创建带有或不带用户模型的配置文件,因此为什么在配置文件表中,用户id属性设置为空。我想存储谁创建和更新的配置文件的id取决于一个警卫(网络或管理员)。我尝试过多态关系,但尚未成功
我通过添加两个变形属性来更新表配置文件
Schema::table('profiles', function (Blueprint $table) {
$table->nullableMorphs('creatable');
$table->nullableMorphs('updatable');
});
我想存储模型id和类型,这取决于自动使用trait的用户保护。
CreateUpdateBy Trait文件是
trait CreatedUpdatedBy
{
public static function bootCreatedUpdatedBy()
{
static::creating(function (Model $model) {
if(auth()->check()) {
//query goes here
}
});
/* filter by profile id */
static::updating(function (Model $model) {
if(auth()->check()) {
//query goes here
}
});
}
}
配置文件模型
class Profile extends BaseModel
{
use CreateUpdateBy;
public function creatable()
{
return $this->morphTo();
}
public function updatable()
{
return $this->morphTo();
}
}
class Admin extends Authenticatable
{
use Notifiable, HasRoles,SoftDeletes;
/* Define guard to use */
protected $guard = 'admin';
public function created_by()
{
return $this->morphMany(Profile::class,'creatable');
}
public function updated_by()
{
return $this->morphMany(Profile::class,'updatable');
}
}
class User extends Authenticatable implements MustVerifyEmail
{
public function profile()
{
return $this->hasOne(Profile::class);
}
public function creatable()
{
return $this->morphOne(Profile::class,'creatable','creatable_type','creatable_id');
}
public function updatable()
{
return $this->morphOne(Profile::class,'updatable','updatable_type','updatable_id');
}
}
管理模式
class Profile extends BaseModel
{
use CreateUpdateBy;
public function creatable()
{
return $this->morphTo();
}
public function updatable()
{
return $this->morphTo();
}
}
class Admin extends Authenticatable
{
use Notifiable, HasRoles,SoftDeletes;
/* Define guard to use */
protected $guard = 'admin';
public function created_by()
{
return $this->morphMany(Profile::class,'creatable');
}
public function updated_by()
{
return $this->morphMany(Profile::class,'updatable');
}
}
class User extends Authenticatable implements MustVerifyEmail
{
public function profile()
{
return $this->hasOne(Profile::class);
}
public function creatable()
{
return $this->morphOne(Profile::class,'creatable','creatable_type','creatable_id');
}
public function updatable()
{
return $this->morphOne(Profile::class,'updatable','updatable_type','updatable_id');
}
}
用户模型
class Profile extends BaseModel
{
use CreateUpdateBy;
public function creatable()
{
return $this->morphTo();
}
public function updatable()
{
return $this->morphTo();
}
}
class Admin extends Authenticatable
{
use Notifiable, HasRoles,SoftDeletes;
/* Define guard to use */
protected $guard = 'admin';
public function created_by()
{
return $this->morphMany(Profile::class,'creatable');
}
public function updated_by()
{
return $this->morphMany(Profile::class,'updatable');
}
}
class User extends Authenticatable implements MustVerifyEmail
{
public function profile()
{
return $this->hasOne(Profile::class);
}
public function creatable()
{
return $this->morphOne(Profile::class,'creatable','creatable_type','creatable_id');
}
public function updatable()
{
return $this->morphOne(Profile::class,'updatable','updatable_type','updatable_id');
}
}
我是如何利用这种特质来实现这一目标的,或者如果我已经准备好学习另一种方法的话