LARAVEL

How to build a Project Management App in Laravel 5 – Part 8


Let’s provide the ability to add Comments to the various tasks.

Create a new folder named comments in the views directory and create a form.blade.php inside the comments directory.

Move the part of the show.blade.php that has to do with the comment form into form.blade.php file like so:

form.blade.php

Replace the part in show.blade.php with @include('comments.form') like so:

Go to routes.php and add this:

Now, let’s create a Comment Model.

 

Create a ProjectCommentsController.php and add this:

We are going to use the principle of Dependency Injection here by creating an instance of Comment and passing it as an argument to postNewComment function.

Now, let’s write a function to get all the comments that belong to a Project.

Back to Comment Model, The Comment Model will look like this now:

The query scope will return all the comments that belong to just one project.

Go to ProjectController.php

Import the Comment Model by writing use Prego\Comment from the top of the Controller file.

Add this function to retrieve all the comments

Just scroll up the show function. We will call the getComments function in the show function. We want to return all the comments to the projects show view.

Modify the Show function to look like this:

Now, the function returns the project with all its comments to the show view.

the form.blade.php in comments folder will change, because we will add the ability to see all the comments with an edit and delete button.

form.blade.php now looks like this:

Now test out the functionality.

Yaaay! it works.

Now, we need to know who posted the comment. Hold up, we actually already know but there should be a text representation of that on the screen. We would soon start adding collaborators and they would be able to comment on the projects they were added to.

Now go back to the Comment Model. Let’s write a function that will establish the relationship between comments and users. Add this to the Comment Model like so:

Go to the form.blade.php in the comments view directory, it will now become this:

Now, let me explain this section:

$comment->user() calls the user method we defined the Comment Model earlier.

$comment->user()->first() returns the first instance of the collection that is retrieved from the User Model via the user_id that was resolved from the comments table.

$comment->user()->first()->username retrieves the username attribute from the object.

Now,

this just says it should return ‘You’ if it’s the user that posted the comment that owns the project else return the collaborator’s username.

Brace Up, Let’s work on editing a Comment

Create an edit.blade.php in the comments directory and add this:

Let’s go to our routes.php file and add the route for editing the comment like so:

Now that we have added that, let’s go back to ProjectCommentsController and add the getOneComment method like so:

Now, let’s test if it works

 

Awesome!,…Now, let’s add the ability to update.

Go to your routes.php and add this:

We are using Route::put because we want to carry out an update operation.

Go to ProjectCommentsController.php, add this:

It validates the comments field, then updates the comments table with the new comment.

Test out the functionality:

Yaay!!. Now we can update a comment successfully.

Let’s add the ability to delete a comment

Go to the routes.php file and add this:

Head over to ProjectCommentsController.php and add this:

Now, test the functionality.

Boom!!!..Yes, it deletes the comment :smile:

One More thing, You will discover that on any Particular project, we still have comments, tasks and attachments as 0.

Let’s change that ASAP!

Go to your show.blade.php in the projects directory

In this section, we are gonna change it to this below:

We used a PHP built-in count() function, it counts the elements in the $tasks, $comments and $files collection. Yes…it’s that simple!!!

 

Yes!!!, it shows the amount of Tasks, comments and attachments on a project.

Now, pat yourself on the back, grab a coffee, then Netflix and Chill because you have done well.

Sorry we couldn’t deal with Adding Collaborators in this post, We’ll do that in the next post.

Please if you have any questions or observations, let me know in the comments section. :smile:

PROSPER OTEMUYIWA

About PROSPER OTEMUYIWA

Food Ninja, Code Slinger, Technical Trainer, Accidental Writer, Open Source Advocate and Developer Evangelist.