diff --git a/app/Http/Controllers/Concerns/ResolvesCurrentShoppingList.php b/app/Http/Controllers/Concerns/ResolvesCurrentShoppingList.php index 7b6d9cc..762e91d 100644 --- a/app/Http/Controllers/Concerns/ResolvesCurrentShoppingList.php +++ b/app/Http/Controllers/Concerns/ResolvesCurrentShoppingList.php @@ -2,13 +2,16 @@ namespace App\Http\Controllers\Concerns; +use App\Models\ShoppingItem; use App\Models\ShoppingList; +use App\Models\User; use Illuminate\Http\Request; trait ResolvesCurrentShoppingList { protected function currentShoppingList(Request $request): ShoppingList { + /** @var User $user */ $user = $request->user(); abort_if($user === null, 403); @@ -27,9 +30,29 @@ trait ResolvesCurrentShoppingList $sessionId = $request->session()->get('current_shopping_list_id'); $current = $lists->firstWhere('id', $sessionId); + if ($current === null) { + $persistedId = $user->last_active_shopping_list_id; + $current = $lists->firstWhere('id', $persistedId); + } + + if ($current === null) { + $listIds = $lists->pluck('id'); + $lastEditedListId = ShoppingItem::query() + ->whereIn('shopping_list_id', $listIds) + ->orderByDesc('updated_at') + ->orderByDesc('id') + ->value('shopping_list_id'); + $current = $lists->firstWhere('id', $lastEditedListId); + } + if ($current === null) { $current = $lists->first(); - $request->session()->put('current_shopping_list_id', $current->id); + } + + $request->session()->put('current_shopping_list_id', $current->id); + + if ((int) $user->last_active_shopping_list_id !== (int) $current->id) { + $user->forceFill(['last_active_shopping_list_id' => $current->id])->saveQuietly(); } return $current; diff --git a/app/Models/User.php b/app/Models/User.php index 864dd27..1f00a6c 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -4,6 +4,7 @@ namespace App\Models; use Illuminate\Contracts\Auth\MustVerifyEmail; use Database\Factories\UserFactory; +use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Factories\HasFactory; @@ -63,4 +64,9 @@ class User extends Authenticatable implements MustVerifyEmail { return $this->belongsToMany(ShoppingList::class, 'shopping_list_user')->withTimestamps(); } + + public function lastActiveShoppingList(): BelongsTo + { + return $this->belongsTo(ShoppingList::class, 'last_active_shopping_list_id'); + } } diff --git a/database/migrations/2026_04_23_120000_add_last_active_shopping_list_id_to_users_table.php b/database/migrations/2026_04_23_120000_add_last_active_shopping_list_id_to_users_table.php new file mode 100644 index 0000000..9fa443b --- /dev/null +++ b/database/migrations/2026_04_23_120000_add_last_active_shopping_list_id_to_users_table.php @@ -0,0 +1,27 @@ +foreignId('last_active_shopping_list_id') + ->nullable() + ->after('remember_token') + ->constrained('shopping_lists') + ->nullOnDelete(); + }); + } + + public function down(): void + { + Schema::table('users', function (Blueprint $table) { + $table->dropForeign(['last_active_shopping_list_id']); + $table->dropColumn('last_active_shopping_list_id'); + }); + } +};