Store selected space in session, display it in navigation and make it switchable through a menu
MODIFY
app/Http/Controllers/LoginController.php
app/Http/Controllers/LoginController.php
@@ -15,6 +15,10 @@ class LoginController extends Controller {
'email' => $request->input('email'),
'password' => $request->input('password')
])) {
+ $user = Auth::user();
+
+ session(['space' => $user->spaces[0]]);
+
return redirect()->route('dashboard');
} else {
$request->flash();

MODIFY
app/User.php
app/User.php
@@ -16,6 +16,10 @@ class User extends Authenticatable {
'password', 'remember_token',
];
+ public function spaces() {
+ return $this->belongsToMany(Space::class, 'user_space');
+ }
+
public function earnings() {
return $this->hasMany(Earning::class);
}

MODIFY
resources/views/layout.blade.php
resources/views/layout.blade.php
</li>
</ul>
</li>
+ <li class="dropdown">
+ <a href="#" class="dropdown__toggle">
+ {{ str_limit(session('space')->name, 3) }} <i class="fas fa-caret-down fa-sm"></i>
+ </a>
+ <ul class="dropdown__list">
+ @foreach (Auth::user()->spaces as $space)
+ <li>
+ <a href="/spaces/{{ $space->id }}">{{ $space->name }}</a>
+ </li>
+ @endforeach
+ </ul>
+ </li>
<li class="dropdown">
<a href="#" class="dropdown__toggle">
<img src="{{ Auth::user()->avatar ? '/storage/avatars/' . Auth::user()->avatar : 'http://placehold.it/50x50' }}" class="avatar mr-05" /> <i class="fas fa-caret-down fa-sm"></i>

MODIFY
routes/web.php
routes/web.php
@@ -41,6 +41,16 @@ Route::group(['middleware' => ['auth']], function () {
Route::get('/settings', 'SettingsController@index')->name('settings');
Route::post('/settings', 'SettingsController@store');
+
+ Route::get('/spaces/{id}', function ($id) {
+ // TODO CHECK IF SPACE IS ACCESSIBLE BY USER
+
+ $space = App\Space::find($id);
+
+ session(['space' => $space]);
+
+ return redirect()->route('dashboard');
+ });
});
Route::get('/logout', 'LogoutController@index')->name('logout');

Store selected space in session, display it in navigation and make it switchable through a menu