Tutorial: Building a support ticket system with laravel and lucid architecture part 5.
Laravel has been fun, i try to make each tutorial very easy to understand, that is why we shared it into features and jobs
We have worked on showing the create view to the users and also storing the inputs of the users. to acheive the above, we used the jobs below
- CreateTicketJob
- StoreTicketJob
- ValidateTicketInputJob
- SendBackWithErrorMessageJob
What are we going to learn
- how to list all items from a repository
- how to build the ListAllTicketsByAuthUser Feature
you can take a look at the course curriculum to catch up with us.
Requirements
the following are the requirements for this tutorial
- Download php 7.2.1
- Download Composer.exe
- Download xampp or wamp for windows.
Difficulty level
This tutorial is rated as intiermiate
What is the task for today.
Our task today is to create the ListTicketByAuthUserFeature. This Feature is responsible for listing all the tickets a particular user has basically we are building this view below.
Zoooooom lets kickoff
Same as the last feature, we start from the route
Route::get('my_tickets', 'TicketController@userTickets');
this sends data to the TicketController@userTickets Function, lets open up our ticket controller and serve the feature, which is ListTicketByAuthUserFeature, remember that we haven created this feature, that is where lucid CLI comes handy.
public function userTickets()
{
return $this->serve(ListTicketByAuthUserFeature::class);
}
head over to lucid command line and generate the feature with the command below
lucid make:feature ListTicketByAuthUserFeature web
Booom magic, feature generated
Okay lets pause and think what does our feature needs to achieve its task,
yeah i know, it suppose to need a job to get all the authenticated users tickets, its category and show to the user. To do this, it has to access the the model Category and Ticket via their repository.
So we need to create the following
- We already created the Ticket Repo in our last tutorial.
- We need the category repository and lastly
- We need the ListAllCategoryJob.
Ok time to create the category repository. Create the CategoryRepository in Src/Data/Repositories
paste the code below
<?php
namespace App\Data\Repositories;
use Framework\Category;
/**
* Base Repository.
*/
class CategoryRepository extends Repository
{
/**
* The model instance.
*
* @var \Illuminate\Database\Eloquent\Model
*/
public $model;
public function __construct()
{
$this->model = new Category;
parent::__construct($this->model);
}
}
Next up job
we are to generate one jobs
lucid make:job ListCategoryJob Catetegory
open the job in Src/Domains/Categories/Jobs/ListCategoryJob
paste the code below
<?php
namespace App\Domains\Categories\Jobs;
use Lucid\Foundation\Job;
use App\Data\Repositories\CategoryRepository;
class ListAllCategoryJob extends Job
{
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
}
/**
* Execute the job.
*
* @return void
*/
public function handle(CategoryRepository $repo)
{
return $repo->all();
}
}
Coupling the Feature
open up the Feature ListTicketByAuthUserFeature
which we generated before, and paste this
<?php
namespace App\Services\Web\Features;
use Lucid\Foundation\Feature;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use App\Domains\Categories\Jobs\ListAllCategoryJob;
use Illuminate\Support\Facades\Auth;
class ListTicketByAuthUserFeature extends Feature
{
public function handle(Request $request)
{
try {
$tickets = Auth::user()->tickets()->paginate(10);
$categories = $this->run(ListAllCategoryJob::class);
return view('tickets.user_tickets', compact('tickets', 'categories'));
} catch (ValidationException $e)
{
}
}
}
OKay take a look at line 17, we saved all the tickets to a viarable from the relationship betwen users and ticket we created in part 2
previous series below
- part 1
- part 2
- part 3
- part 4
line 22 send this data $tickets and $categories to a view which we would create inviews/tickets/user_tickets
below is the view
@extends('layouts.app')
@section('title', 'My Tickets')
@section('content')
<div class="row">
<div class="col-md-10 col-md-offset-1">
<div class="panel panel-default">
<div class="panel-heading">
<i class="fa fa-ticket"> My Tickets</i>
</div>
<div class="panel-body">
@if ($tickets->isEmpty())
<p>You have not created any tickets.</p>
@else
<table class="table">
<thead>
<tr>
<th>Category</th>
<th>Title</th>
<th>Status</th>
<th>Last Updated</th>
</tr>
</thead>
<tbody>
@foreach ($tickets as $ticket)
<tr>
<td>
@foreach ($categories as $category)
@if ($category->id === $ticket->category_id)
{{ $category->name }}
@endif
@endforeach
</td>
<td>
<a href="{{ url('tickets/'. $ticket->ticket_id) }}">
#{{ $ticket->ticket_id }} - {{ $ticket->title }}
</a>
</td>
<td>
@if ($ticket->status === 'Open')
<span class="label label-success">{{ $ticket->status }}</span>
@else
<span class="label label-danger">{{ $ticket->status }}</span>
@endif
</td>
<td>{{ $ticket->updated_at }}</td>
</tr>
@endforeach
</tbody>
</table>
{{ $tickets->render() }}
@endif
</div>
</div>
</div>
</div>
@endsection
finally thats all for today, bamm!!! we have this view
Posted on Utopian.io - Rewarding Open Source Contributors
Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]
Hey @manishmike10, I just gave you a tip for your hard work on moderation. Upvote this comment to support the utopian moderators and increase your future rewards!
Hey @sirfreeman I am @utopian-io. I have just upvoted you!
Achievements
Suggestions
Get Noticed!
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x