Empty Collection ManyToMany Polymorphic, Mongo DB
I am trying to create a ManyToMany Polymorphic relationship in Laravel, I am using MongoDB
This is my Role Class: namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Jenssegers\Mongodb\Eloquent\Model;
class Role extends Model { use HasFactory;
/**
* The attributes that are mass assignable
*
* @var array
*/
protected $fillable = ['name', 'description'];
/**
* Get all of the permissions for the Role
*/
public function permissions()
{
return $this->morphToMany(Permission::class, 'permissionable');
}
}
This is my Permissions Class: namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Jenssegers\Mongodb\Eloquent\Model;
class Permission extends Model { use HasFactory;
/**
* The attributes that are mass assignable
*
* @var array
*/
protected $fillable = [ 'name' ];
/**
* Get all of the roles that are assigned this permission
*/
public function roles()
{
return $this->morphedByMany(Role::class, 'permissionable');
}
/**
* Get all of the users that are assigned additionally to this permission
*/
public function users()
{
return $this->morphedByMany(User::class, 'permissionable');
}
/**
* Get the policy that owns the permission
*/
public function policy()
{
return $this->belongsTo(Policy::class);
}
}
This is my MIgration for polymorphic table:
/** * Run the migrations * * @return void */ public function up() { Schema::create('permissionable', function (Blueprint $table) { $table->id(); $table->foreignId('permission_id')->constrained('permissions'); $table->unsignedBigInteger('permissionable_id'); $table->unsignedBigInteger('permissionable_type'); $table->boolean('active'); $table->timestamps(); }); }
/**
* Reverse the migrations
*
* @return void
*/
public function down()
{
Schema::dropIfExists('permissionable');
}
This is example of one of the recordings in permissionable table DB: {"_id":{"$oid":"60ebfd00f8440000dc004826"},"permission_id":"60ebfd00f8440000dc00481a","permissionable_id":"60ebfd00f8440000dc004812","permissionable_type":"App\Models\Role","active":1}
this is one example of permission table : {"_id":{"$oid":"60ebfd00f8440000dc00481a"},"name":"viewAny","policy_id":"60ebfd00f8440000dc004815","active":1,"updated_at":{"$date":"2021-07-12T08:27:44,722Z"},"created_at":{"$date":"2021-07-12T08:27:44 722Z"}}
this is one example of role table: {"_id":{"$oid":"60ebfd00f8440000dc004812"},"name":"Administrator","description":"Super Admin, has every permission "}
This is what tinker returns: Role::first()->permissions => Illuminate\Database\Eloquent\Collection {#4413 all: [], }
What am I doing wrong?
Please or to participate in this conversation.