• We just launched and are currently in beta. Join us as we build and grow the community.

Laravel: Comment on a Post using AJAX

HeroSolstice

Online Course Monetizer
H Rep
0
0
0
Rep
0
H Vouches
0
0
0
Vouches
0
Posts
99
Likes
99
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
Getting Started

This is a continuation of my previous tutorial, which you can visit, entitled Laravel User Post using AJAX.

Creating our Model

We will create our Comment model as well as our migration.

1. In command prompt, navigate to our project and type:

php artisan make:model Comment -m

This will create our model Comment.php located in app folder. It will also create the migration for us due to the -m that we added in creating the model located in database/migrations folder. It will be something like create_comments_table.php.

2. Since we are going to migrate this specific migration, we are going to transfer it to a folder so that the --path in our php artisan can locate this file. You can do this by creating a new folder in database/migration and name it as selected.

3. Open our transferred file and edit it with the ff codes:

  1. <?php

  2. use

    Illuminate\Support\Facades\Schema;
  3. use

    Illuminate\Database\Schema\Blueprint;
  4. use

    Illuminate\Database\Migrations\Migration;

  5. class

    CreateCommentsTable extends

    Migration
  6. {
  7. /**
  8. * Run the migrations.
  9. *
  10. * @return void
  11. */
  12. public

    function

    up(

    )
  13. {
  14. Schema::

    create

    (

    'comments'

    ,

    function

    (

    Blueprint $table

    )

    {
  15. $table

    ->

    increments

    (

    'id'

    )

    ;
  16. $table

    ->

    integer

    (

    'userid'

    )

    ;
  17. $table

    ->

    integer

    (

    'postid'

    )

    ;
  18. $table

    ->

    text

    (

    'comment'

    )

    ;
  19. $table

    ->

    timestamps

    (

    )

    ;
  20. }

    )

    ;
  21. }

  22. /**
  23. * Reverse the migrations.
  24. *
  25. * @return void
  26. */
  27. public

    function

    down(

    )
  28. {
  29. Schema::

    dropIfExists

    (

    'comments'

    )

    ;
  30. }
  31. }

Migrating our Newly Created Migration

In command prompt, navigate to our project and type:

php artisan migrate --path=/database/migrations/selected/

This will migrate all the files in our --path folder.

Updating our Routes

Next we update our routes to accommodate our comment function. This is our new web.php:

  1. <?php

  2. Route::

    get

    (

    '/'

    ,

    function

    (

    )

    {
  3. return

    view(

    'welcome'

    )

    ;
  4. }

    )

    ;

  5. Auth::

    routes

    (

    )

    ;

  6. Route::

    get

    (

    '/home'

    ,

    'HomeController@index'

    )

    ->

    name

    (

    'home'

    )

    ;

  7. Route::

    post

    (

    '/post'

    ,

    'PostController@post'

    )

    ;

  8. Route::

    get

    (

    '/show'

    ,

    'PostController@getPost'

    )

    ;

  9. Route::

    get

    (

    '/getcomment'

    ,

    'PostController@getComment'

    )

    ;

  10. Route::

    post

    (

    '/writecomment'

    ,

    'PostController@makeComment'

    )

    ;

Update and Create New Views

Update/Create the ff blades:

home.blade.php
  1. @

    extends

    (

    'layouts.app'

    )

  2. @

    section(

    'content'

    )
  3. <

    div class

    =

    "container"

    >
  4. <

    h1 class

    =

    "page-header text-center"

    >

    Laravel User Post using AJAX</

    h1>
  5. @

    if

    (

    session(

    'status'

    )

    )
  6. <

    div class

    =

    "alert alert-success"

    >
  7. {

    {

    session(

    'status'

    )

    }

    }
  8. </

    div>
  9. @

    endif
  10. <

    div class

    =

    "row"

    >
  11. <

    div class

    =

    "col-md-8 col-md-offset-2"

    >
  12. <

    div class

    =

    "panel panel-default"

    >
  13. <

    div class

    =

    "panel-body"

    >
  14. <

    form id=

    "postForm"

    >
  15. <

    textarea class

    =

    "form-control"

    name=

    "post"

    id=

    "post"

    placeholder=

    "What's on your mind?"

    ></

    textarea>
  16. <

    button type=

    "button"

    id=

    "postBtn"

    class

    =

    "btn btn-primary"

    style=

    "margin-top:5px;"

    ><

    i class

    =

    "fa fa-pencil-square-o"

    ></

    i>

    POST</

    button>
  17. </

    form>
  18. </

    div>
  19. </

    div>
  20. <

    div id=

    "postList"

    ></

    div>
  21. </

    div>
  22. </

    div>
  23. </

    div>
  24. @

    endsection

  25. @

    section(

    'script'

    )
  26. <

    script type=

    "text/javascript"

    >
  27. $(

    document)

    .

    ready(

    function

    (

    )

    {
  28. $.

    ajaxSetup(

    {
  29. headers:

    {
  30. 'X-CSRF-TOKEN'

    :

    $(

    'meta[name="csrf-token"]'

    )

    .

    attr(

    'content'

    )
  31. }
  32. }

    )

    ;

  33. showPost(

    )

    ;

  34. $(

    '#postBtn'

    )

    .

    click(

    function

    (

    )

    {
  35. var

    post =

    $(

    '#post'

    )

    .

    val(

    )

    ;
  36. if

    (

    post==

    ''

    )

    {
  37. alert(

    'Please write a Post first!'

    )

    ;
  38. $(

    '#post'

    )

    .

    focus(

    )

    ;
  39. }
  40. else

    {
  41. var

    postForm =

    $(

    '#postForm'

    )

    .

    serialize

    (

    )

    ;
  42. $.

    ajax(

    {
  43. type:

    'POST'

    ,
  44. url:

    '/post'

    ,
  45. data:

    postForm,
  46. dataType:

    'json'

    ,
  47. success:

    function

    (

    )

    {
  48. showPost(

    )

    ;
  49. $(

    '#postForm'

    )

    [

    0

    ]

    .

    reset

    (

    )

    ;
  50. }

    ,
  51. }

    )

    ;
  52. }
  53. }

    )

    ;

  54. $(

    document)

    .

    on(

    'click'

    ,

    '.comment'

    ,

    function

    (

    )

    {
  55. var

    id =

    $(

    this)

    .

    val(

    )

    ;
  56. if

    (

    $(

    '#commentField_'

    +

    id)

    .

    is(

    ':visible'

    )

    )

    {
  57. $(

    '#commentField_'

    +

    id)

    .

    slideUp(

    )

    ;
  58. }
  59. else

    {
  60. $(

    '#commentField_'

    +

    id)

    .

    slideDown(

    )

    ;
  61. getComment(

    id)

    ;
  62. }
  63. }

    )

    ;

  64. $(

    document)

    .

    on(

    'click'

    ,

    '.submitComment'

    ,

    function

    (

    )

    {
  65. var

    id =

    $(

    this)

    .

    val(

    )

    ;
  66. if

    (

    $(

    '#commenttext'

    )

    .

    val(

    )

    ==

    ''

    )

    {
  67. alert(

    'Please write a Comment First!'

    )

    ;
  68. }
  69. else

    {
  70. var

    commentForm =

    $(

    '#commentForm_'

    +

    id)

    .

    serialize

    (

    )

    ;
  71. $.

    ajax(

    {
  72. type:

    'POST'

    ,
  73. url:

    'writecomment'

    ,
  74. data:

    commentForm,
  75. success:

    function

    (

    )

    {
  76. getComment(

    id)

    ;
  77. $(

    '#commentForm_'

    +

    id)

    [

    0

    ]

    .

    reset

    (

    )

    ;
  78. }

    ,
  79. }

    )

    ;
  80. }

  81. }

    )

    ;

  82. }

    )

    ;

  83. function

    showPost(

    )

    {
  84. $.

    ajax(

    {
  85. url:

    '/show'

    ,
  86. success:

    function

    (

    data)

    {
  87. $(

    '#postList'

    )

    .

    html(

    data)

    ;
  88. }

    ,
  89. }

    )

    ;
  90. }

  91. function

    getComment(

    id)

    {
  92. $.

    ajax(

    {
  93. url:

    'getcomment'

    ,
  94. data:

    {

    id:

    id}

    ,
  95. success:

    function

    (

    data)

    {
  96. $(

    '#comment_'

    +

    id)

    .

    html(

    data)

    ;
  97. }
  98. }

    )

    ;
  99. }
  100. </script>
  101. @

    endsection

postlist.blade.php

  1. @

    foreach

    (

    $posts

    as

    $post

    )
  2. <

    div class

    =

    "panel panel-default"

    >
  3. <

    div class

    =

    "panel-body"

    >
  4. <

    p style=

    "font-size:16px;"

    ><

    b>

    {

    {

    $post

    ->

    name

    }

    }

    </

    b>

    added a post.</

    p>
  5. <

    p style=

    "font-size:11px; margin-top:-10px;"

    >

    {

    {

    date

    (

    'M d, Y h:i A'

    ,

    strtotime

    (

    $post

    ->

    created_at

    )

    )

    }

    }

    </

    p>
  6. <

    h3 style=

    "padding-top:30px; padding-bottom:30px;"

    >

    {

    {

    $post

    ->

    post

    }

    }

    </

    h3>
  7. </

    div>
  8. <

    div class

    =

    "panel-footer"

    >
  9. <

    div class

    =

    "row"

    >
  10. <

    div class

    =

    "col-md-2"

    >
  11. <

    button class

    =

    "btn btn-primary btn-sm"

    ><

    i class

    =

    "fa fa-thumbs-up"

    ></

    i>

    <

    span>

    Like</

    span></

    button>
  12. </

    div>
  13. <

    div class

    =

    "col-md-10"

    style=

    "margin-left:-40px;"

    >
  14. <

    button type=

    "button"

    class

    =

    "btn btn-primary btn-sm comment"

    value=

    "{{ $post->postid

    }}"

    ><

    i class

    =

    "fa fa-comments"

    ></

    i>

    button>
  15. </

    div>
  16. </

    div>
  17. </

    div>
  18. </

    div>
  19. <

    div id=

    "commentField_{{ $post->postid

    }}"

    class

    =

    "panel panel-default"

    style=

    "padding:10px; margin-top:-20px; display:none;"

    >
  20. <

    div id=

    "comment_{{ $post->postid

    }}"

    >
  21. </

    div>
  22. <

    form id=

    "commentForm_{{ $post->postid

    }}"

    >
  23. <

    input type=

    "hidden"

    value=

    "{{ $post->postid

    }}"

    name=

    "postid"

    >
  24. <

    div class

    =

    "row"

    >
  25. <

    div class

    =

    "col-md-10"

    >
  26. <

    input type=

    "text"

    name=

    "commenttext"

    id=

    "commenttext"

    data-

    id=

    "{{ $post->postid

    }}"

    class

    =

    "form-control commenttext"

    placeholder=

    "Write a Comment..."

    >
  27. </

    div>
  28. <

    div class

    =

    "col-md-2"

    style=

    "margin-left:-25px;"

    >
  29. <

    button type=

    "button"

    class

    =

    "btn btn-primary submitComment"

    value=

    "{{ $post->postid

    }}"

    ><

    i class

    =

    "fa fa-comment"

    ></

    i>

    Submit</

    button>
  30. </

    div>
  31. </

    div>

  32. </

    form>
  33. </

    div>
  34. @

    endforeach

commentlist.blade.php
  1. @

    foreach

    (

    $comments

    as

    $comment

    )
  2. <

    div >
  3. <

    b>

    {

    {

    $comment

    ->

    name

    }

    }

    </

    b>:

    {

    {

    $comment

    ->

    comment

    }

    }
  4. <

    p style=

    "font-size:9px;"

    >

    {

    {

    date

    (

    'M d, Y h:i A'

    ,

    strtotime

    (

    $comment

    ->

    created_at

    )

    )

    }

    }

    </

    p>
  5. </

    div>
  6. @

    endforeach

Updating our Controller

Next, update our PostController.php with the ff:

  1. <?php

  2. namespace

    App\Http\Controllers;

  3. use

    Illuminate\Http\Request;
  4. use

    DB;
  5. use

    Auth;
  6. use

    App\Post;
  7. use

    App\Comment;

  8. class

    PostController extends

    Controller
  9. {
  10. public

    function

    getPost(

    )

    {
  11. $posts

    =

    DB::

    table

    (

    'posts'

    )
  12. ->

    join

    (

    'users'

    ,

    'users.id'

    ,

    '='

    ,

    'posts.userid'

    )
  13. ->

    select

    (

    'posts.id as postid'

    ,

    'posts.*'

    ,

    'users.id as userid'

    ,

    'users.*'

    )
  14. ->

    orderBy

    (

    'posts.created_at'

    ,

    'desc'

    )
  15. ->

    get

    (

    )

    ;

  16. return

    view(

    'postlist'

    ,

    compact

    (

    'posts'

    )

    )

    ;
  17. }

  18. public

    function

    post(

    Request $request

    )

    {
  19. if

    (

    $request

    ->

    ajax

    (

    )

    )

    {
  20. $user

    =

    Auth::

    user

    (

    )

    ;
  21. $post

    =

    new

    Post;

  22. $post

    ->

    userid

    =

    $user

    ->

    id

    ;
  23. $post

    ->

    post

    =

    $request

    ->

    input

    (

    'post'

    )

    ;

  24. $post

    ->

    save

    (

    )

    ;

  25. return

    response(

    $post

    )

    ;
  26. }
  27. }

  28. public

    function

    getComment(

    Request $request

    )

    {
  29. if

    (

    $request

    ->

    ajax

    (

    )

    )

    {
  30. $comments

    =

    DB::

    table

    (

    'comments'

    )
  31. ->

    join

    (

    'users'

    ,

    'users.id'

    ,

    '='

    ,

    'comments.userid'

    )
  32. ->

    select

    (

    'comments.id as commentid'

    ,

    'comments.*'

    ,

    'users.id as userid'

    ,

    'users.*'

    )
  33. ->

    where

    (

    'postid'

    ,

    '='

    ,

    $request

    ->

    id

    )
  34. ->

    get

    (

    )

    ;

  35. return

    view(

    'commentlist'

    ,

    compact

    (

    'comments'

    )

    )

    ;
  36. }
  37. }

  38. public

    function

    makeComment(

    Request $request

    )

    {
  39. if

    (

    $request

    ->

    ajax

    (

    )

    )

    {
  40. $user

    =

    Auth::

    user

    (

    )

    ;
  41. $comment

    =

    new

  42. $comment

    ->

    userid

    =

    $user

    ->

    id

    ;
  43. $comment

    ->

    postid

    =

    $request

    ->

    postid

    ;
  44. $comment

    ->

    comment

    =

    $request

    ->

    commenttext

    ;

  45. $comment

    ->

    save

    (

    )

    ;

  46. return

    response(

    $comment

    )

    ;
  47. }
  48. }
  49. }

Running our Server

In your web browser, type the name that you added in localhost for your project in my case, post.dev. You should now be able to comment on any post.

That ends this tutorial. Happy Coding :)


Download
You must upgrade your account or reply in the thread to view hidden text.
 

450,491

323,080

323,089

Top