Tutorial: Building a support ticket system with laravel and lucid architecture part 5.

in #utopian-io7 years ago (edited)

cover-Recovered.jpg

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

  1. how to list all items from a repository
  2. 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.

userticket.png

Zoooooom lets kickoff

Same as the last feature, we start from the route

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

web.png

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

  1. We already created the Ticket Repo in our last tutorial.
  2. We need the category repository and lastly
  3. 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);

    }

   
}

ticketrepo.png

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();
    }
}

listcategoryJob.png

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)
        {


        }
    }
}

image.png

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 in views/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

userticket.png

finally thats all for today, bamm!!! we have this view



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

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

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

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

Coin Marketplace

STEEM 0.18
TRX 0.16
JST 0.030
BTC 67614.76
ETH 2610.64
USDT 1.00
SBD 2.73