Build A Support Ticket Application With Laravel — Part 2

This is the second and concluding part of Build A Support Ticket Application With Laravel. In part 1, we setup the application and created our first ticket. In case you missed it, you should check Build A Support Ticket Application With Laravel — Part 1 out because we will be continuing from where we stopped.

Displaying User Tickets

We start off by allowing our users to see a list of all the tickets they have created and from there they can go on to view a particular ticket. Add the code below to TicketsController

// TicketsController.php // Remember to add the lines below at the top of the controller // use App\User; // use App\Ticket; // use App\Category; // use Illuminate\Support\Facades\Auth; public function userTickets() { $tickets = Ticket::where('user_id', Auth::user()->id)->paginate(10); $categories = Category::all(); return view('tickets.user_tickets', compact('tickets', 'categories')); }

We are getting the tickets created by the authenticated user by passing Auth::user()->id to the where() and getting only the 10 tickets to be displayed in a page by using Laravel paginate() and passing 10 to it.

This will prevent us from showing all tickets created by a user in a single page which can be awkward if the user has lots of tickets. The tickets together with the categories are passed to a view file which will we will create shortly. Before creating the view file, let’s create the routes that will handle displaying all tickets created by a user. Add the line below to routes.php

Route::get('my_tickets', 'TicketsController@userTickets');

With the routes created, lets go on and create the view. Create a new view file named user_tickets.blade.php in the tickets folder and paste the code below to it:


Originally published at www.laravelfeed.com.