Creating a support ticket with laravel lucid architecture part 6

in #utopian-io7 years ago (edited)

cover-Recovered-Recovered.jpg

In our last tutorial, we learnt how to create the ListTicketsByAuthUser Feature, the feature that is responsible for listing all the tickets by the authenticated user. To achieve the previous task, we built the following;

  1. We needed the category repository and lastly
  2. We needed the ListAllCategoryJob.
    Here we are again, another feature, ListTicketByIdFeature
    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 intermediate.

What's our task for today?

Our tasks today is to build the ListTicketByIdFeature, which is responsible for getting a specific users ticket using the id in the model to fetch the ticket

to achieve this we are going to build the LIstTicketByIdJob, as the name implies, the jobs list the ticket using the id.
We are going to learn how to pass the id from a model to a feature.

Lets begin

We going to start by initiating the route
Ok route where are you, yeep it in web.php of the web service. Head over there.
add the code below

Route::get('tickets/{ticket_id}', 'TicketController@show');

This time around, we are passing the ticket_id as a variable to the TicketController's show method. Our route url would look like localhost:8000/tickets/1

Passing the ticket_id to the controller

Open the ticket controller and add the code below in the show method
the remember to use the ListTicketByIdFeature at the top
use App\Services\Web\Features\ListTicketByIdFeature;

 */
    public function show($ticket_id)
    {
        return $this->serve(ListTicketByIdFeature::class,[

            'id' => $ticket_id,
        ]);
    }

Let's explain this piece of code.
We are setting the id in the ListTicketFeature to the ticket_id of the route from the ticket model in the constructor method of the ListTicketFeature.

Feature time!!!! (ListTicketByIdFeature)

We would start by hitting the command line to generate our feature

Enter lucid make:feature ListTicketById web
Boom!!!!!!!!! generated.

Open the generated feature
and add the code.

<?php
namespace App\Services\Web\Features;

use Lucid\Foundation\Feature;
use Illuminate\Http\Request;
use App\Domains\Tickets\Jobs\ListTicketByIdJob;

class ListTicketByIdFeature extends Feature
{
    public function __construct($id)
    {
        $this->id = $id;
    }

    public function handle(Request $request)
    {
        $ticket = $this->run(ListTicketByIdJob::class,[

            'id' => $this->id,
            'field' => 'ticket_id'
        ]);

        $category = $ticket->category;

        $comments = $ticket->comments;

    

    return view('tickets.show', compact('ticket', 'category', 'comments'));
    }
}

Now, lets talk about this section
Notice the construct accepts the id we passed from the controller.
In this section below, we are running a ListTicketByJob and we don't have this particular job

$ticket = $this->run(ListTicketByIdJob::class,[

            'id' => $this->id,
            'field' => 'ticket_id'
        ]);

So we are going create the job

lucid make:job ListTicketByIdJob

find the generated job at Domains/Tickets/Jobs/ListTicketByIdJob.php

paste below snippet

<?php
namespace App\Domains\Tickets\Jobs;

use Lucid\Foundation\Job;
use App\Data\Repositories\TicketRepository;

class ListTicketByIdJob extends Job
{
   /**
    * Create a new job instance.
    *
    * @return void
    */
   protected $id;
   protected $field;


   public function __construct($id, $field)
   {
       $this->id = $id;
       $this->field = $field;
    
   }

   /**
    * Execute the job.
    *
    * @return void
    */
   public function handle(TicketRepository $repo)
   {
       return $repo->model->where($this->field, $this->id)->firstOrFail();
   }

}   

This jobs go to the repository and get the tickets with the id , the job, uses a the where method of the repository class, which accepts two variables, a ticket itselt, and the ID we are looking for.

Back to the Feature (ListTicketByIdFeature)

We ran the ListTicketJob, next we save the category of a particular ticket to the variable $category through the relationship between tickets and category.

lastly the both data "$ticket and $category" are sent to the tickets/show.blade.php view



@extends('layouts.app')

@section('title', $ticket->title)

@section('content')
    <div class="row">
        <div class="col-md-10 col-md-offset-1">
            <div class="panel panel-default">
                <div class="panel-heading">
                    #{{ $ticket->ticket_id }} - {{ $ticket->title }}
                </div>

                <div class="panel-body">
                    @include('includes.flash')

                    <div class="ticket-info">
                        <p>{{ $ticket->message }}</p>
                        <p>Categry: {{ $category->name }}</p>
                        <p>
                        @if ($ticket->status === 'Open')
                            Status: <span class="label label-success">{{ $ticket->status }}</span>
                        @else
                            Status: <span class="label label-danger">{{ $ticket->status }}</span>
                        @endif
                        </p>
                        <p>Created on: {{ $ticket->created_at->diffForHumans() }}</p>
                    </div>

                    <hr>

                        <div class="comments">
                            @foreach ($comments as $comment)
                                <div class="panel panel-@if($ticket->user->id === $comment->user_id) {{"default"}}@else{{"success"}}@endif">
                                    <div class="panel panel-heading">
                                        {{ $comment->user->name }}
                                        <span class="pull-right">{{ $comment->created_at->format('Y-m-d') }}</span>
                                    </div>

                                    <div class="panel panel-body">
                                        {{ $comment->comment }}     
                                    </div>
                                </div>
                            @endforeach
                        </div>

                    <div class="comment-form">
                        <form action="{{ url('comment') }}" method="POST" class="form">
                            {!! csrf_field() !!}

                            <input type="hidden" name="ticket_id" value="{{ $ticket->id }}">

                            <div class="form-group{{ $errors->has('comment') ? ' has-error' : '' }}">
                                <textarea rows="10" id="comment" class="form-control" name="comment"></textarea>

                                @if ($errors->has('comment'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('comment') }}</strong>
                                    </span>
                                @endif
                            </div>

                            <div class="form-group">
                                <button type="submit" class="btn btn-primary">Submit</button>
                            </div>
                        </form>
                </div>
            </div>
        </div>
    </div>
@endsection

Booom, yayayaya, we done.

previous series below



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

You got upvoted from @adriatik bot! Thank you to you for using our service. We really hope this will hope to promote your quality content!

You got a 31.14% upvote from @allaz courtesy of @sirfreeman!

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!
  • You are generating more rewards than average for this category. Super!;)
  • 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.21
TRX 0.26
JST 0.039
BTC 96251.98
ETH 3374.21
USDT 1.00
SBD 3.04