feat(outlet user): menambahkan migrasi dan model untuk relasi many to many
This commit is contained in:
parent
99d5858641
commit
968626b4de
@ -33,11 +33,6 @@ public function getSlugOptions(): SlugOptions
|
||||
->saveSlugsTo('slug');
|
||||
}
|
||||
|
||||
public function users(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(User::class, 'user_outlet')->withPivot('position_id');
|
||||
}
|
||||
|
||||
public function openingHours(): HasMany
|
||||
{
|
||||
return $this->hasMany(OpeningHour::class);
|
||||
@ -72,4 +67,9 @@ public function expenses(): HasMany
|
||||
{
|
||||
return $this->hasMany(Expense::class);
|
||||
}
|
||||
|
||||
public function users(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(User::class);
|
||||
}
|
||||
}
|
||||
|
||||
12
app/Models/OutletUser.php
Normal file
12
app/Models/OutletUser.php
Normal file
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class OutletUser extends Model
|
||||
{
|
||||
protected $table = 'outlet_user';
|
||||
|
||||
protected $guarded = ['id'];
|
||||
}
|
||||
@ -7,6 +7,7 @@
|
||||
use App\Enums\UserStatus;
|
||||
use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
@ -52,4 +53,9 @@ public function referralUsage(): HasOne
|
||||
{
|
||||
return $this->hasOne(ReferralUsage::class);
|
||||
}
|
||||
|
||||
public function outlets(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(Outlet::class);
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('outlet_user', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignId('outlet_id')->constrained()->cascadeOnDelete();
|
||||
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
|
||||
$table->timestamp('created_at')->useCurrent();
|
||||
$table->timestamp('updated_at')->useCurrent()->useCurrentOnUpdate();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('outlet_user');
|
||||
}
|
||||
};
|
||||
@ -6,6 +6,7 @@
|
||||
use App\Enums\Gender;
|
||||
use App\Enums\UserRole;
|
||||
use App\Models\Employee;
|
||||
use App\Models\Outlet;
|
||||
use App\Models\ReferralCode;
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Seeder;
|
||||
@ -39,5 +40,9 @@ public function run(): void
|
||||
]);
|
||||
|
||||
$user->assignRole(UserRole::Developer);
|
||||
|
||||
$outlet_ids = Outlet::all()->pluck('id')->toArray();
|
||||
|
||||
$user->outlets()->attach($outlet_ids);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user