Search code examples
phplaravellaravel-5ticket-system

query results of signed in user in laravel


I currently have a ticket system built and I would like users to view the status of their Open and Closed tickets.

I can query the database and get all tickets to display, but I only want the logged in users tickets to show. How would I achieve this. Been at it a few days now and I'm at a loss.

What I have:

ROUTE (This returns a 404 due to the {ticketId}) / Note: If I remove the {ticketId} from the Route I get the Type error: *Too few arguments to function App\Http\Controllers\TicketsController::openTickets(), 0 passed and exactly 1 expected"*

Route::get('/tickets/open-tickets/{ticketId}','TicketsController@openTickets')->name('open-tickets');

TICKETS CONTROLLER

public function openTickets($ticketId){

$tickets=Classified::find($ticketId);
$ticketId = Auth::user('id');
$tickets = DB::table('tickets')->orderBy('st_id', 'DESC')->where('status', '=', 'OPEN')->where('user_id', '=', $id)->paginate(4);
return view('tickets.open-tickets',compact('tickets'));

Ticket MODEL

class Ticket extends Model
{
    use Notifiable, HasPermissionsTrait;

    protected $ticketId = 'st_id';
    protected $guarded=[];  

    protected $dispatchesEvents = [
     'created' => Events\NewTicket::class
 ];
}

WORKING QUERY TO DISPLAY ALL TICKETS

public function openTickets(){
    $tickets = DB::table('tickets')->orderBy('st_id', 'DESC')->where('status', '=', 'OPEN')->paginate(4);
return view('tickets.open-tickets',compact('tickets'));

I should mention that I changed my id to st_id on the tickets table as I'm using the ID to display the ticket number like this 201804001

Again I'm just looking for a solution to have the logged in user view their tickets and NOT all tickets in the database.

Image of Tickets Table: enter image description here


Solution

  • You already have a Ticket model but you're using the DB facade to query the tickets table.

    Instead, associate Tickets with Users via a one-to-many relationship.

    class Ticket extends Model
    {
        public function user()
        {
            return $this->belongsTo(User::class);
        }
    }
    

    In your User class:

        public function tickets()
        {
            return $this->hasMany(Ticket::class);
        }
    

    Now you can do $user->tickets()->where('status', 'OPEN')->get(); to get all the open tickets for that user.

    Even further, you can add a scope to the Ticket model.

    In your Ticket class:

    public function scopeOpen($query)
    {
        return $query->where('status', 'OPEN');
    }
    

    This will allow you to make your queries more fluent:

    $user->tickets()->open()->get();

    To associate a ticket with a user, simply do something along the following lines:

    $user->tickets()->save(new Ticket([
        'status' => 'open',
        'title'  => 'Problem logging in',
        'body'   => 'Lorem ipsum dolor sit amet'
    ]));
    

    For more information about Eloquent relationships (specifically one-to-many relationships), see the documentation here.

    To list all of a user's open tickets, use the following route:

    Route::get('tickets/open-tickets/', 'TicketsController@index');

    To show an individual ticket, you'll want the following route:

    Route::get('tickets/open-tickets/{ticket}', 'TicketsController@show')

    Use the following methods in your TicketsController:

    public function index()
    {
        // user() is a helper for Auth::user()
        $tickets = user()->tickets()->open()->paginate(4);
    
        return view('tickets.open-tickets', compact('tickets'));
    }
    
    public function show(Ticket $ticket)
    {
        // If the ticket does not belong to the logged in user, abort
        if ($ticket->user_id != user()->id) {
            abort(403, 'This is not your ticket');
        }
    
        return view('tickets.show', compact('ticket'));
    }
    

    You'll notice that the show method in your controller type-hints the Ticket model. This is called route-model binding, which is very useful and you can read more about that here.

    It may seem like I'm overcomplicating things compared to your original code, but trust me, it's better to understand these things early on! They'll help you a whole lot during development.