Skip to content

Add a commentable structure to any model

MisterDebug edited this page May 20, 2023 · 4 revisions

With the command make:commentable, you can add a commentable to any model.

Example in real life, you want to add comments on a Post model

Let's create your Post CRUD : image

Imagine, you want to add the comment block in your view posts/show.blade.php

@extends('default')

@section('content')
    <h1>{{ $post->title }}</h1>
    {!! nl2br($post->content) !!}
@stop

Now let's add the comment block

Add somewhere in your show page {{comment_here}} like this :

    <h1>{{ $post->title }}</h1>
    {!! nl2br($post->content) !!}
    <br><br>
    {{comment_here}}

image

And voilà, you have a comment block in your show page

image

Add theses routes in your web.php

Route::post('comments/store/{post}', [CommentsController::class, 'store'])->name('comments.store');
Route::delete('comments/{id}', [CommentsController::class, 'destroy'])->name('comments.destroy');

Enjoy :)

Clone this wiki locally