Skip to content

Commit ca732fb

Browse files
committed
Displaying logged-in user notes
1 parent e080d2a commit ca732fb

File tree

8 files changed

+108
-5
lines changed

8 files changed

+108
-5
lines changed

app/Http/Controllers/AuthController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function loginSubmit(Request $request)
8484

8585
// echo '<pre>';
8686
// print_r($user);
87-
echo 'LOGIN REALIZADO COM SUCESSO!';
87+
return redirect()->to('/');
8888
}
8989

9090
public function logout()

app/Http/Controllers/MainController.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,22 @@
22

33
namespace App\Http\Controllers;
44

5+
use App\Models\User;
56
use Illuminate\Http\Request;
67

78
class MainController extends Controller
89
{
910
public function index()
1011
{
11-
echo "I'm inside the app!";
12+
$id = session('user.id');
13+
// $user = User::find($id)->toArray();
14+
$notes = User::find($id)->notes()->get()->toArray();
15+
16+
// echo '<pre>';
17+
// print_r($user);
18+
// print_r($notes);
19+
20+
return view('home', ['notes' => $notes]);
1221
}
1322

1423
public function newNote()

app/Models/Note.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class Note extends Model
8+
{
9+
public function user(){
10+
return $this->belongsTo(User::class);
11+
}
12+
}

app/Models/User.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,7 @@
66

77
class User extends Model
88
{
9+
public function notes(){
10+
return $this->hasMany(Note::class);
11+
}
912
}

resources/views/home.blade.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
@extends('layouts.main_layout')
2+
3+
@section('content')
4+
<div class="container mt-5">
5+
<div class="row justify-content-center">
6+
<div class="col">
7+
8+
@include('top_bar')
9+
10+
@if (count($notes) == 0)
11+
<!-- no notes available -->
12+
<div class="row mt-5">
13+
<div class="col text-center">
14+
<p class="display-6 mb-5 text-secondary opacity-50">You have no notes available!</p>
15+
<a href="{{ route('new') }}" class="btn btn-secondary btn-lg p-3 px-5">
16+
<i class="fa-regular fa-pen-to-square me-3"></i>Create Your First Note
17+
</a>
18+
</div>
19+
</div>
20+
21+
<!-- temp -->
22+
<hr class="my-5">
23+
24+
@else
25+
<!-- notes are available -->
26+
<div class="d-flex justify-content-end mb-3">
27+
<a href="{{ route('new') }}" class="btn btn-secondary px-3">
28+
<i class="fa-regular fa-pen-to-square me-2"></i>New Note
29+
</a>
30+
</div>
31+
32+
@foreach ($notes as $note)
33+
@include('note')
34+
@endforeach
35+
36+
@endif
37+
</div>
38+
</div>
39+
</div>
40+
@endsection

resources/views/note.blade.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<div class="row">
2+
<div class="col">
3+
<div class="card p-4">
4+
<div class="row">
5+
<div class="col">
6+
<h4 class="text-info">{{ $note['title'] }}</h4>
7+
<small class="text-secondary"><span class="opacity-75 me-2">Created at:</span><strong>{{ $note['created_at'] }}</strong></small>
8+
</div>
9+
<div class="col text-end">
10+
<a href="#" class="btn btn-outline-secondary btn-sm mx-1"><i class="fa-regular fa-pen-to-square"></i></a>
11+
<a href="#" class="btn btn-outline-danger btn-sm mx-1"><i class="fa-regular fa-trash-can"></i></a>
12+
</div>
13+
</div>
14+
<hr>
15+
<p class="text-secondary">{{ $note['text'] }}</p>
16+
</div>
17+
</div>
18+
</div>

resources/views/top_bar.blade.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<div class="row mb-3 align-items-center">
2+
<div class="col">
3+
<a href="{{ route('home') }}">
4+
<img src="assets/images/logo.png" alt="Notes logo">
5+
</a>
6+
7+
</div>
8+
<div class="col text-center">
9+
A simple <span class="text-warning">Laravel</span> project!
10+
</div>
11+
<div class="col">
12+
<div class="d-flex justify-content-end align-items-center">
13+
<span class="me-3"><i class="fa-solid fa-user-circle fa-lg text-secondary me-3"></i>[username]</span>
14+
<a href="{{ route('logout') }}" class="btn btn-outline-secondary px-3">
15+
Logout<i class="fa-solid fa-arrow-right-from-bracket ms-2"></i>
16+
</a>
17+
</div>
18+
</div>
19+
</div>
20+
21+
<hr>

routes/web.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
});
1313

1414
Route::middleware([CheckIsLogged::class]) ->group(function(){
15-
Route::get('/' , [ MainController::class, 'index' ]);
16-
Route::get('newNote' , [ MainController::class, 'newNote' ]);
17-
Route::get('logout' , [ AuthController::class, 'logout' ]);
15+
Route::get('/' , [ MainController::class, 'index' ])->name('home');
16+
Route::get('newNote' , [ MainController::class, 'newNote' ])->name('new');
17+
Route::get('logout' , [ AuthController::class, 'logout' ])->name('logout');
1818
});

0 commit comments

Comments
 (0)