Laravel ticketing system

Let’s Implement a Simple Ticketing System With Laravel

Ticketing systems needed by every corporation these days so in this tutorial we will implement a simple ticketing system using the laravel framework.

 

 

Requirements:

  • Laravel 5 preferable version 5.5
  • Mysql

 

First of all create a new laravel project using composer as follows:

After installation complete make sure you have .env file otherwise copy file .env.example and rename it to .env

set your application key:

Navigate to http://localhost/support-ticket/public/. Well the application is running with the default landing page.

 

Sometimes when run the website for the first time you might see a blank page. To solve such problem give writable permissions to directories such as storage/ and bootstrap/.

 

Now let’s go to phpmyadmin and create a new database called “ticketing”.

Then open your .env file and set your database credentials as shown:

Now let’s list the database tables that we will need in our application.

  • Tickets: This is the main table for storing tickets.
  • Comments: This table responsible for storing user comments and replies.
  • Categories: This table responsible for storing ticket categories so user will select category when creating ticket.

We will use laravel migrations to create our tables so right in the terminal type the following:

The above commands will create three models along with their migrations for tables tickets, comments, and categories.

Now let’s populate the migration files.

Go to the ticket migration file located at database/migrations/ and modify as follows:

In the same way open the comment migration file and modify:

Next open the category migration file and modify as follows:

Also we need to modify the create_users migration as follows:

 

Now run the migration to create the tables.

Setting the relationships:

Now we need to setup the relationships between our models. So if we imagine the scenario we we end up with this structure:

  • Relationship between tickets and comments: Ticket has many comments and comment belong to specific ticket.
  • Relationship between tickets and user: Ticket belong to one user and user can create many tickets.
  • Relationship between tickets and categories: Ticket belong to one category and category has many tickets.
  • Relationship between comments and users: Comment belong to one user and user has many comments.

app/Ticket.php

app/Comment.php

app/Category.php

app/User.php

As shown in the code above we established the relationships using laravel’s helper functions for relations such as hasMany, belongsTo.

Now its time to setup authentication.

 

Setting Up Authentication

Let’s generate authentication routes as laravel helps us with this with single command and you have ready to use authentication system:

Now if you visit your application there are two links appearing at the top for login form and register form like this:

laravel authetication

Now we have to implement the ticket controller

 

Preparing Application Controllers:

Let’s create a new controller that will manipulate  tickets display and creation.

In the above command we created a resource controller with the basic functions. Now remove actions edit, update, and destroy as it will not be needed.

Let’s add this line in the constructor:

This line tells laravel to allow only authenticated users to view and create tickets.

 

In the way create the comments controller:

app/Http/Controllers/CommentsController.php

Admin Middleware

At this point we need a way for each user to manage his tickets so we can add admin page for this but with laravel’s middleware this is pretty easy. We can do this by adding a middleware so for example our tickets page can be accessed by http://localhost/projectname/admin/tickets.

Create a new class in this path app/Http/Middleware called AdminMiddleware.php with this contents:

app/Http/Middleware/AdminMiddleware.php

In the above code i checked if the user not login or he is login but not is_admin then we redirect him to the landing page else we proceed.

 

app/Http/Kernel.php

 

To apply this middleware we will add it to our routes as shown below:

 

Preparing Application Routes:

Now we will add our required routes so open routes/web.php and modify it like this:

routes/web.php

 

Tickets Controller:

Now the first thing we need to do is to manage and create user tickets. This will be done in the index and user_tickets function so let’s modify our ticket controller like this:

app/Http/Controllers/TicketsController.php

For the view create a new folder in the views directory called tickets and inside it create file index.blade.php with the following contents:

resources/views/tickets/index.blade.php (Admin tickets)

resources/views/tickets/user_tickets.blade.php (User tickets)

In the above code we created two views the first view will display all tickets so that the system admin can view them and reply or close for each one. The other view will display user related tickets.

First we check if the tickets not empty else we displayed a message to the user that no tickets exist. If there are tickets then we display tickets by looping over them and display them with ticket title, category and status. We also add buttons for commenting and closing the ticket.

 

To test this scenario we will need two accounts. One admin account which has is_admin=1 and the other is user account which has is_admin=0 as shown:

laravel ticketing system

laravel ticketing system

laravel ticketing system

Now login with admin account and navigate to http://localhost/support-ticket/public/admin/tickets and you will see “There are currently no tickets.“.

Logout and login with user account and navigate to http://localhost/support-ticket/public/my_tickets and you will see “You have not created any tickets yet.“.

Creating Tickets Form

Let’s create our tickets form to enable users to create new ticket. Go to app/Http/Controllers/TicketsController.php and modify it like this:

app/Http/Controllers/TicketsController.php

app/Mailers/AppMailer.php

resources/views/tickets/create.blade.php

resources/views/emails/ticket_info.blade.php

In the above code first in the TicketsController we added the code that displays the create form and in the store method we validate and submit the form.

This line send mail to the user with ticket information  to notify him that a new ticket created. For this we added simple class app/Mailers/AppMailer.php to send enable users to send mails.

Note that to test this scenario you can use dummy mail service like Mailtrap. Go to https://mailtrap.io and create a new account it will give you some credentials take them and update your .env file like this:

After that now you ready to test creating tickets just login with the user account and navigate to http://localhost/support-ticket/public/new-ticket and fill the form and click submit as shown:

laravel ticketing system

laravel ticketing system

Now go to http://localhost/support-ticket/public/my_tickets.

 

Displaying A Single Ticket

To display single ticket and enable user to reply and comment in the ticket we will update the show method in the TicketsController as shown below:

resources/views/tickets/show.blade.php

resources/views/tickets/comments.blade.php

resources/views/tickets/reply.blade.php

In the above code First we display the ticket info like category, status then we display the comments and replies located in comments.blade.php finally the reply form.

Replying The Tickets

To add reply on a ticket this will be done in the CommentsController so modify it as shown:

app/Http/Controllers/CommentsController.php

app/Mailers/AppMailer.php

resources/views/emails/ticket_comments.blade.php

Now in the ticket page try to add a reply and click submit you will see that the reply appear in the comments list with user and time like this:

laravel ticketing system

 

Closing The Ticket

Its important for the system admin to close the ticket once resolved. To do this we will update the TicketsController and add the close method like this:

app/Http/Controllers/TicketsController.php

app/Mailers/AppMailer.php

resources/views/emails/ticket_status.blade.php

In the above code first we update the status to closed then we sent the user a notification email that his ticket closed then redirect to the tickets page.

laravel ticketing system

Updating Navigation

To enable users and admins for easy navigation to the tickets page we will update the home view as follows:

resources/views/home.blade.php

 

Share this: