소스 검색

project relationship order

Andrew 2 달 전
부모
커밋
2266908b13

+ 1
- 1
app/Filament/Resources/BadgeResource.php 파일 보기

@@ -34,7 +34,7 @@ class BadgeResource extends Resource
34 34
     protected static function booted()
35 35
     {
36 36
         static::addGlobalScope('ordered', function ($query) {
37
-            $query->orderBy('order')->orderBy('id');
37
+            $query->orderBy('badges.order')->orderBy('badges.id');
38 38
         });
39 39
     }
40 40
 

+ 12
- 7
app/Filament/Resources/ProjectResource.php 파일 보기

@@ -20,7 +20,6 @@ use Filament\Forms\Components\Tabs\Tab;
20 20
 use Filament\Forms\Components\Textarea;
21 21
 use Filament\Forms\Components\TextInput;
22 22
 use Filament\Forms\Form;
23
-use Filament\Forms\Get;
24 23
 use Filament\Resources\Resource;
25 24
 use Filament\Tables;
26 25
 use Filament\Tables\Columns\ImageColumn;
@@ -108,7 +107,10 @@ class ProjectResource extends Resource
108 107
                                 ->searchable()
109 108
                                 ->label('')
110 109
                                 ->required(),
111
-                        ])->reorderable(false),
110
+                        ])
111
+                            ->relationship('targetBadges')
112
+                            ->reorderableWithButtons()
113
+                            ->orderColumn('sort_order'),
112 114
                         Repeater::make('badgesAward')->label('取得標章')->schema([
113 115
                             Hidden::make('award_type')->default(2),
114 116
                             Select::make('badge_id')
@@ -133,7 +135,10 @@ class ProjectResource extends Resource
133 135
                                 ->displayFormat('Y年m月')
134 136
                                 ->native(false)
135 137
                                 ->closeOnDateSelection(),
136
-                        ])->reorderable(false),
138
+                        ])
139
+                            ->relationship('awardBadges')
140
+                            ->reorderableWithButtons()
141
+                            ->orderColumn('sort_order'),
137 142
                     ]),
138 143
                     Tab::make('開發歷程')->schema([
139 144
                         Repeater::make('histories')->label('')->schema([
@@ -155,16 +160,16 @@ class ProjectResource extends Resource
155 160
                                 ])->columnSpanFull()
156 161
                                 ->id(fn ($get) => 'histories_'.$get('histories_item_key')),
157 162
                         ])
158
-                            ->relationship('histories', function ($query, Get $get, $livewire) {
159
-                                return $query->orderby('operate_date', 'desc');
160
-                            })
163
+                            ->relationship('histories')
164
+                            ->reorderableWithButtons()
165
+                            ->orderColumn('order')
161 166
                             ->collapsible()
162 167
                             ->cloneable(),
163 168
                     ]),
164 169
                     Tab::make('空間資訊')->schema([
165 170
                         Group::make()->schema([
166 171
                             Translate::make()->schema(fn (string $locale) => [
167
-//                                TextInput::make('contact_unit')->label('物管單位'),
172
+                                //                                TextInput::make('contact_unit')->label('物管單位'),
168 173
                                 TextInput::make('contact_phone')->label('物管電話'),
169 174
                                 TextInput::make('inversment_phone')->label('招商電話'),
170 175
                             ])

+ 8
- 5
app/Models/Project.php 파일 보기

@@ -35,7 +35,8 @@ class Project extends Model
35 35
     public function badges()
36 36
     {
37 37
         return $this->morphToMany(Badge::class, 'badgeable')
38
-            ->withPivot('award_date', 'award_type')  // ✅ 關鍵
38
+            ->withPivot('award_date', 'award_type', 'sort_order')
39
+            ->orderByPivot('sort_order')
39 40
             ->withTimestamps();
40 41
     }
41 42
 
@@ -45,8 +46,9 @@ class Project extends Model
45 46
     public function targetBadges()
46 47
     {
47 48
         return $this->morphToMany(Badge::class, 'badgeable')
48
-            ->withPivot('award_type')
49
+            ->withPivot('award_type', 'sort_order')
49 50
             ->wherePivot('award_type', 1)
51
+            ->orderByPivot('sort_order')
50 52
             ->withTimestamps();
51 53
     }
52 54
 
@@ -56,19 +58,20 @@ class Project extends Model
56 58
     public function awardBadges()
57 59
     {
58 60
         return $this->morphToMany(Badge::class, 'badgeable')
59
-            ->withPivot('award_type', 'award_date')
61
+            ->withPivot('award_type', 'award_date', 'sort_order')
60 62
             ->wherePivot('award_type', 2)
63
+            ->orderByPivot('sort_order')
61 64
             ->withTimestamps();
62 65
     }
63 66
 
64 67
     public function histories()
65 68
     {
66
-        return $this->hasMany(ProjectHistory::class);
69
+        return $this->hasMany(ProjectHistory::class)->orderBy('order');
67 70
     }
68 71
 
69 72
     public function spaceInfos()
70 73
     {
71
-        return $this->hasMany(ProjectSpaceInfo::class);
74
+        return $this->hasMany(ProjectSpaceInfo::class)->orderBy('order');
72 75
     }
73 76
 
74 77
     public function firstListImgUrl(): Attribute

+ 28
- 0
database/migrations/2025_12_02_075717_add_order_column_to_badgeables_table.php 파일 보기

@@ -0,0 +1,28 @@
1
+<?php
2
+
3
+use Illuminate\Database\Migrations\Migration;
4
+use Illuminate\Database\Schema\Blueprint;
5
+use Illuminate\Support\Facades\Schema;
6
+
7
+return new class extends Migration
8
+{
9
+    /**
10
+     * Run the migrations.
11
+     */
12
+    public function up(): void
13
+    {
14
+        Schema::table('badgeables', function (Blueprint $table) {
15
+            $table->integer('order')->default(0)->after('award_type');
16
+        });
17
+    }
18
+
19
+    /**
20
+     * Reverse the migrations.
21
+     */
22
+    public function down(): void
23
+    {
24
+        Schema::table('badgeables', function (Blueprint $table) {
25
+            $table->dropColumn('order');
26
+        });
27
+    }
28
+};

+ 28
- 0
database/migrations/2025_12_02_075722_add_order_column_to_project_histories_table.php 파일 보기

@@ -0,0 +1,28 @@
1
+<?php
2
+
3
+use Illuminate\Database\Migrations\Migration;
4
+use Illuminate\Database\Schema\Blueprint;
5
+use Illuminate\Support\Facades\Schema;
6
+
7
+return new class extends Migration
8
+{
9
+    /**
10
+     * Run the migrations.
11
+     */
12
+    public function up(): void
13
+    {
14
+        Schema::table('project_histories', function (Blueprint $table) {
15
+            $table->integer('order')->default(0)->after('visible');
16
+        });
17
+    }
18
+
19
+    /**
20
+     * Reverse the migrations.
21
+     */
22
+    public function down(): void
23
+    {
24
+        Schema::table('project_histories', function (Blueprint $table) {
25
+            $table->dropColumn('order');
26
+        });
27
+    }
28
+};

+ 28
- 0
database/migrations/2025_12_02_090453_rename_order_to_sort_order_in_badgeables_table.php 파일 보기

@@ -0,0 +1,28 @@
1
+<?php
2
+
3
+use Illuminate\Database\Migrations\Migration;
4
+use Illuminate\Database\Schema\Blueprint;
5
+use Illuminate\Support\Facades\Schema;
6
+
7
+return new class extends Migration
8
+{
9
+    /**
10
+     * Run the migrations.
11
+     */
12
+    public function up(): void
13
+    {
14
+        Schema::table('badgeables', function (Blueprint $table) {
15
+            $table->renameColumn('order', 'sort_order');
16
+        });
17
+    }
18
+
19
+    /**
20
+     * Reverse the migrations.
21
+     */
22
+    public function down(): void
23
+    {
24
+        Schema::table('badgeables', function (Blueprint $table) {
25
+            $table->renameColumn('sort_order', 'order');
26
+        });
27
+    }
28
+};