4 use Illuminate\Database\Migrations\Migration;
5 use Illuminate\Database\Schema\Blueprint;
6 use Illuminate\Support\Facades\DB;
7 use Illuminate\Support\Facades\Schema;
9 return new class extends Migration
14 public function up(): void
16 Schema::create('attachments', function (Blueprint $table) {
17 $table->increments('id');
18 $table->string('name');
19 $table->string('path');
20 $table->string('extension', 20);
21 $table->integer('uploaded_to');
23 $table->boolean('external');
24 $table->integer('order');
26 $table->integer('created_by');
27 $table->integer('updated_by');
29 $table->index('uploaded_to');
33 // Get roles with permissions we need to change
34 $adminRoleId = DB::table('roles')->where('system_name', '=', 'admin')->first()->id;
36 // Create & attach new entity permissions
37 $ops = ['Create All', 'Create Own', 'Update All', 'Update Own', 'Delete All', 'Delete Own'];
38 $entity = 'Attachment';
39 foreach ($ops as $op) {
40 $permissionId = DB::table('role_permissions')->insertGetId([
41 'name' => strtolower($entity) . '-' . strtolower(str_replace(' ', '-', $op)),
42 'display_name' => $op . ' ' . $entity . 's',
43 'created_at' => Carbon::now()->toDateTimeString(),
44 'updated_at' => Carbon::now()->toDateTimeString(),
46 DB::table('permission_role')->insert([
47 'role_id' => $adminRoleId,
48 'permission_id' => $permissionId,
54 * Reverse the migrations.
56 public function down(): void
58 Schema::dropIfExists('attachments');
60 // Create & attach new entity permissions
61 $ops = ['Create All', 'Create Own', 'Update All', 'Update Own', 'Delete All', 'Delete Own'];
62 $entity = 'Attachment';
63 foreach ($ops as $op) {
64 $permName = strtolower($entity) . '-' . strtolower(str_replace(' ', '-', $op));
65 DB::table('role_permissions')->where('name', '=', $permName)->delete();