Skip to content

Instantly share code, notes, and snippets.

@Neeraj1005
Created July 16, 2022 03:55
Show Gist options
  • Save Neeraj1005/496d5576c5dfe1c1ac35f2bee27d6586 to your computer and use it in GitHub Desktop.
Save Neeraj1005/496d5576c5dfe1c1ac35f2bee27d6586 to your computer and use it in GitHub Desktop.
spatie query builder uses for calling scope and includes

Controller

$tickets = QueryBuilder::for(Ticket::class)
            ->allowedFilters(['ticket_number', 
                                'name', 
                                'status_id', 
                                'type_id', 
                                'priority_id', 
                                'user_id', 
                                AllowedFilter::partial('ticketStatus.name'),
                                AllowedFilter::partial('priority.name'),
                                AllowedFilter::partial('users.id'),
                                AllowedFilter::scope('withUnassignedTickets'),
                            ])
            ->allowedIncludes(['ticketStatus', 'ticketType', 'users', 'priority'])
            ->paginate(10)
            ->appends(request()->query());

Model

    public function scopeWithUnassignedTickets($query) {

        return $query->where('user_id','=',Null);
    }
    
    public function priority() {

        return $this->belongsTo( 'App\Priority','priority_id');
    }
    
    public function users() {

        // return $this->belongsTo('App\User', 'user_id')->where('active',1);
        return $this->belongsTo('App\User', 'user_id');
    }
    
    public function ticketStatus() {

        return $this->belongsTo( 'App\Ticket_status', 'status_id' );
    }

Blade file url

<a href="{{ route('admin.ticket',['filter[withUnassignedTickets]'=> 'Null']) }}"
    class="btn btn-outline-secondary mr-1">
    Unassigned
</a>

@foreach($status as $status_item)
    <a
        href="{{ route('admin.ticket', ['filter[ticketStatus.name]' => $status_item->name]) }}"
        class="btn btn-outline-secondary mr-1">
        {{ $status_item->name }}
    </a>
@endforeach
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment