LARAVEL

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


Create a collaborators directory in the resources/views directory and create a form.blade.php.

In the form.blade.php, add this:

Then go to your show.blade.php in your projects directory and add

@include('collaborators.form') just immediately after the div that gives us information about the number of comments, tasks and attachments like so:

So, when you navigate to a particular project, it should be like this:

 

Now that we have done that.

Let’s add facebook type of mention to our project. So that once you @ and specify a user’s handle, it can suggest several potential users for collaboration.

Luckily, I have written a package for that called laravel-mentions.  So you can read the documentation and use it.

Open your composer.json file, add this in the require section:

Run composer update.

Then go to the config/app.php and add this:

to the providers array.

After doing that, then run php artisan vendor:publish from your terminal. That will copy some files to your project like mentions.php and a js file.

Go to your config/mentions.php file now and change App\User to Prego\User.

Head over to master.blade.php file and add this:

You can grab jquery.atwho.js here, jquery.caret.js here and jquery.atwho.min.css here and place them in your js and css folders respectively.

Once you have done this, go to your collaborators/form.blade.php and replace it with this:

Here

According to the package documentation, users refers to the users table and username refer to the column of the users table. That’s where we want the auto-suggest data to come from.

Now go to your routes.php, let’s add a route for adding Collaborators.

Let’s create a Collaboration Model

Collaboration.php

Now, create ProjectCollaboratorsController.php in Http/Controllers directory and add this:

ProjectCollaboratorsController.php

Let’s break this down for better understanding.

This line of code below gets the collaborator’s username and yanks off the @ symbol.

So the username is passed to the getId function to retrieve the id

This isCollaborator method checks if the user is already a collaborator on this particular project.

So if it returns a result other than null, then he/she is already a collaborator…so it sends an appropriate warning message that the user can not be added a second time.

 

This code below checks if the potential collaborator exists as a user or not. If someone types in a random value in the text field for adding new collaborators..then the result would be null and it will give an appropriate error message.

Let’s test these functionalities.

 

 

Awesome!!..All our checks are working!

Now, let’s add the ability to see the collaborator’s avatar.

collaborators/form.blade.php  will now be:

In this code below: I am calling the user() function on $collaborator which is an instance of Collaboration model.

Therefore, head over to the Collaboration Model and add this:

Now, I am putting collaborator_id as the second argument there because that’s the foreign_key that establishes a relationship between the Collaboration and User Model. By default, it will try to look for user_id, but I used collaborator_id in place of that.

Now, we also need to add the queryScope method to the Collaboration Model that will return information about the current project a user might be working on.

Go to ProjectController.php and add this method:

 

Now, we have to return information about the collaborators to the page through the show method of the ProjectController.

show method will look like this now:

So, try out the functionality now:

 

 

Yaay!, adding a collaborator worked!

Now, let’s check deleting of a collaborator

 

That also worked. :smile:

Remaining Features

– When a collaborator is added to a project, he/she should receive an email.

– When a collaborator logs in, he/she should be able to see the project that he has been invited to collaborate on, maybe a badge on the project that shows it’s a shared project.

– A Collaborator on a project should be able to see the project, comment on the project, add tasks and attachments to the project.

– A user should be able to add/edit and delete todos.

– A user should be able to update his/her account.

– Hosting on Heroku

Those are the remaining features listed above for us to be done with this very simple and basic project management application.

Let’s Quickly treat sending of emails before ending this blog post.

Sending Email When A User is added as a Collaborator on a Project

I like to use Mandrill when dealing with transactional emails, so you can go ahead and create an account with Mandrill. Go to the settings page, create an api key, that will serve as a your password. The host, username , driver details are all on that page.

Go to your .env file and add those details like so:

where xxxxx is your username and password respectively.

Now, we are gonna use the concept of Model Events and Observers.

Laravel gives you the luxury of listening to Model events such as saved, saving, deleted, deleting, created, creating, saving, saved, restored, restoring out of the box.

Don’t be worried. It’s actually so simple, nothing complex at all.

Head over to providers/EventServiceProvider.php file, we are gonna put our logic in the boot method. We will listen to the saved method of the Collaboration method. So add this:

So once a user has been added as a collaborator, it sends an email to the collaborator notifying that he/she has been added as a collaborator.

Do not forget to add these:

at the top in the EventServiceProvider.php file

Go to ProjectCollaboratorsController.php to add this two methods:

We need these two methods to get the project name and the email of the collaborator that will be added to the project.

So add this just before $collaboration->save() in addCollaborator method.

So that our EventServiceProvider can have access to project_name and user_email

Also, create an emails directory, create a file called collaborate.blade.php.

Add this to the file:

This is our email template text, it takes in $username and $project_name variables. Those variables are passed in from the $data array that we declared before the Mail::send method.

Try out the functionality now

Yaaay!, It works! :smile:

Please let me know if you have any questions or observation in the comments section below.

 

 

PROSPER OTEMUYIWA

About PROSPER OTEMUYIWA

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