#create_users_table
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
}
#create_panels_table
public function up()
{
Schema::create('panels', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
}
#create_user_panel_table
public function up()
{
Schema::create('user_panel', function (Blueprint $table) {
$table->integer('user_id')->unsigned();
$table->integer('panel_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')
->onDelete('cascade');
$table->foreign('panel_id')->references('id')->on('panels')
->onDelete('cascade');
});
}
#User Model
public function panels(): BelongsToMany
{
return $this->belongsToMany(Panel::class, 'user_panel');
}
#Panel Model
public function users(): BelongsToMany
{
return $this->belongsToMany(User::class, 'user_panel');
}
#Controller
public function getUserPanels($userId){
$user = User::find($userId);
return $user->panels;
}
public function getPanelUsers($panelId){
$panel = Panel::find($panelId);
return $panel->users;
}