Move logic for switching spaces into separate controller
ADD
app/Http/Controllers/SpaceController.php
+<?php
+
+namespace App\Http\Controllers;
+
+use App\Space;
+use Illuminate\Http\Request;
+
+class SpaceController extends Controller {
+ public function __invoke($id) {
+ $space = Space::find($id);
+
+ if ($space->users->contains(Auh::user()->id)) {
+ return redirect()->route('dashboard');
+ }
+
+ session(['space' => $space]);
+
+ return redirect()->route('dashboard');
+ }
+}

MODIFY
routes/web.php
routes/web.php
@@ -59,17 +59,7 @@ Route::group(['middleware' => ['auth']], function () {
Route::get('/settings', 'SettingsController@index')->name('settings');
Route::post('/settings', 'SettingsController@store');
- Route::get('/spaces/{id}', function ($id) {
- $space = App\Space::find($id);
-
- if ($space->users->contains(Auth::user()->id)) {
- return redirect()->route('dashboard');
- }
-
- session(['space' => $space]);
-
- return redirect()->route('dashboard');
- });
+ Route::get('/spaces/{id}', 'SpaceController');
});
Route::get('/logout', 'LogoutController@index')->name('logout');

Move logic for switching spaces into separate controller