From 79ca841d5a6c1c4b18ead0cfa8047ade54bc0f40 Mon Sep 17 00:00:00 2001 From: Axel Pointud <axel.pointud@etu.unistra.fr> Date: Mon, 18 Sep 2023 20:27:47 +0200 Subject: [PATCH] etape 2 : affichage de tous les commentaires --- app/Http/Controllers/CommentController.php | 17 +++++++++++ app/Models/Comment.php | 16 ++++++++++ database/factories/CommentFactory.php | 24 +++++++++++++++ ...023_09_18_180158_create_comments_table.php | 29 +++++++++++++++++++ database/seeders/DatabaseSeeder.php | 3 ++ resources/views/comment/index.blade.php | 26 +++++++++++++++++ routes/web.php | 11 ++++++- 7 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 app/Http/Controllers/CommentController.php create mode 100644 app/Models/Comment.php create mode 100644 database/factories/CommentFactory.php create mode 100644 database/migrations/2023_09_18_180158_create_comments_table.php create mode 100644 resources/views/comment/index.blade.php diff --git a/app/Http/Controllers/CommentController.php b/app/Http/Controllers/CommentController.php new file mode 100644 index 0000000..566d5de --- /dev/null +++ b/app/Http/Controllers/CommentController.php @@ -0,0 +1,17 @@ +<?php + +namespace App\Http\Controllers; + +use App\Models\Comment; +use Illuminate\Http\Request; + +class CommentController extends Controller +{ + public function index() { + $comments = Comment::all(); + + return view('comment.index', [ + 'comments' => $comments + ]); + } +} diff --git a/app/Models/Comment.php b/app/Models/Comment.php new file mode 100644 index 0000000..2766b1c --- /dev/null +++ b/app/Models/Comment.php @@ -0,0 +1,16 @@ +<?php + +namespace App\Models; + +use Illuminate\Database\Eloquent\Factories\HasFactory; +use Illuminate\Database\Eloquent\Model; + +class Comment extends Model +{ + use HasFactory; + + protected $fillable = [ + 'author', + 'content' + ]; +} diff --git a/database/factories/CommentFactory.php b/database/factories/CommentFactory.php new file mode 100644 index 0000000..c0692e7 --- /dev/null +++ b/database/factories/CommentFactory.php @@ -0,0 +1,24 @@ +<?php + +namespace Database\Factories; + +use Illuminate\Database\Eloquent\Factories\Factory; + +/** + * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Comment> + */ +class CommentFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array<string, mixed> + */ + public function definition(): array + { + return [ + 'author' => fake()->name(), + 'content' => fake()->sentence(), + ]; + } +} diff --git a/database/migrations/2023_09_18_180158_create_comments_table.php b/database/migrations/2023_09_18_180158_create_comments_table.php new file mode 100644 index 0000000..1934a1f --- /dev/null +++ b/database/migrations/2023_09_18_180158_create_comments_table.php @@ -0,0 +1,29 @@ +<?php + +use Illuminate\Database\Migrations\Migration; +use Illuminate\Database\Schema\Blueprint; +use Illuminate\Support\Facades\Schema; + +return new class extends Migration +{ + /** + * Run the migrations. + */ + public function up(): void + { + Schema::create('comments', function (Blueprint $table) { + $table->id(); + $table->string('author'); + $table->string('content'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('comments'); + } +}; diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index e30fdfd..687e72e 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -3,6 +3,8 @@ namespace Database\Seeders; // use Illuminate\Database\Console\Seeds\WithoutModelEvents; + +use App\Models\Comment; use Illuminate\Database\Seeder; use App\Models\Post; @@ -14,5 +16,6 @@ class DatabaseSeeder extends Seeder public function run(): void { Post::factory(10)->create(); + Comment::factory(5)->create(); } } diff --git a/resources/views/comment/index.blade.php b/resources/views/comment/index.blade.php new file mode 100644 index 0000000..025ebcd --- /dev/null +++ b/resources/views/comment/index.blade.php @@ -0,0 +1,26 @@ +@extends('base') + +@section('title', 'Commentaires du blog') + + +@section('content') + <a class="text-yellow-500 text-4xl font-bold" href="{{ route('blog.index') }}">Retour</a> + + + <h1 class="text-red-500 font-bold text-4xl">Les commentaires</h1> + @empty($comments) + <p>no comments</p> + @else + <ul> + @foreach ($comments as $comment) + <li class="mb-6"> + <p class="text-green-400 text-sm">{{ $comment->author }}</p> + <p> {{ $comment->content }}</p> + </li> + @endforeach + </ul> + + + + @endempty +@endsection diff --git a/routes/web.php b/routes/web.php index 980c6d5..812ada9 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,6 +1,7 @@ <?php use App\Http\Controllers\BlogController; +use App\Http\Controllers\CommentController; use App\Http\Controllers\ProfileController; use Illuminate\Support\Facades\Route; @@ -15,10 +16,18 @@ | */ + + Route::prefix('/')->controller(BlogController::class)->group(function () { Route::get('/', 'index')->name('blog.index'); - Route::get('/{post}', 'show')->name('blog.show'); + Route::get('/blog/{post}', 'show')->name('blog.show'); + +}); + +Route::prefix('/commentaires')->controller(CommentController::class)->group(function () { + Route::get('/', 'index')->name('comment.index'); + }); -- GitLab