HEX
Server: Apache/2.4.58 (Ubuntu)
System: Linux ip-172-26-0-120 6.17.0-1009-aws #9~24.04.2-Ubuntu SMP Fri Mar 6 23:50:29 UTC 2026 x86_64
User: ubuntu (1000)
PHP: 8.3.6
Disabled: NONE
Upload Files
File: /var/www/html/dashboard.orbiwheels.com/app/DataTables/Transporter/BookingDataTable.php
<?php

namespace App\DataTables\Transporter;

use App\Models\Booking;
use App\Models\Trip;
use Illuminate\Database\Eloquent\Builder as QueryBuilder;
use Yajra\DataTables\EloquentDataTable;
use Yajra\DataTables\Html\Builder as HtmlBuilder;
use Yajra\DataTables\Html\Button;
use Yajra\DataTables\Html\Column;
use Yajra\DataTables\Html\Editor\Editor;
use Yajra\DataTables\Html\Editor\Fields;
use Yajra\DataTables\Services\DataTable;

class BookingDataTable extends DataTable
{
    /**
     * Build the DataTable class.
     *
     * @param QueryBuilder<Booking> $query Results from query() method.
     */
    public function dataTable(QueryBuilder $query): EloquentDataTable
    {
        return (new EloquentDataTable($query))
            ->addIndexColumn()
            ->editColumn('booking_code', function ($row) {
                $url = route('booking.view', encrypt($row->id)); // apna route lagao
                return '<a href="' . $url . '">' . $row->booking_code . '</a>';
            })
            ->addColumn('ride_type', function ($row) {

                return $row->ride_type_name;
            })
            ->addColumn('booking_code', function ($row) {

                return $row->booking_code;
            })
            ->addColumn('total_trips', function ($row) {
                return $row->trips->count();
            })

            ->addColumn('booking_type', function ($row) {
                return $row->booking_type == 1 ? 'One Way' : 'Two Way';
            })
            ->addColumn('no_of_passengers', function ($row) {
                if (!empty($row->no_of_passengers)) {
                    return $row->no_of_passengers ?? '-';
                }
                return '-';
            })
            ->addColumn('status', function ($row) {
                $status = $row->booking_status;
                switch ($status) {
                    case 'pending':
                        return '<span class="badge badge-warning text-white" style="font-size: medium">Pending</span>';
                    case 'assigned':
                        return '<span class="badge badge-dark text-white" style="font-size: medium">Assigned</span>';
                    case 'ongoing':
                        return '<span class="badge badge-info text-white" style="font-size: medium">Ongoing</span>';
                    case 'completed':
                        return '<span class="badge badge-success text-white" style="font-size: medium">Completed</span>';
                    case 'in_progress':
                        return '<span class="badge badge-primary text-white" style="font-size: medium">In Progress</span>';
                    case 'cancelled':
                        return '<span class="badge badge-danger text-white" style="font-size: medium">Cancelled</span>';
                    default:
                        return '<span class="badge badge-secondary text-white" style="font-size: medium">Unknown</span>';
                }
            })
            // ->addColumn('created_at', function ($row) {
            //     return date('d-m-Y', strtotime($row->created_at));
            // })
            ->addColumn('action', function ($row) {
                $buttons = '<div class="table-actions d-flex">';

                $buttons .= '
                        <a href="' . route('booking.view', encrypt($row->id)) . '" class="btn btn-primary btn-sm" title="view">
                            <i class="fa fa-eye"></i>
                        </a>';

                $buttons .= '</div>';

                return $buttons;
            })
            ->rawColumns(['status', 'action', 'booking_code'])
            ->setRowId('id');
    }

    /**
     * Get the query source of dataTable.
     *
     * @return QueryBuilder<Booking>
     */
    public function query(Booking $model): QueryBuilder
    {
        $transporterId = auth()->guard('trans')->user()->id;

        $query = $model->newQuery()
            ->with(['rides', 'customer', 'trips'])
            ->whereHas('trips', function ($q) use ($transporterId) {
                $q->where('status', Trip::ACCEPTED)
                    ->where('transporter_id', $transporterId);
            })
            ->orderBy('created_at', 'desc');

        if (request()->filled('status')) {
            $status = request('status');
            $query->whereHas('rides', function ($q) use ($status) {
                $q->where('status', $status);
            });
        }

        if (request()->filled('booking_type')) {
            $query->where('booking_type', request('booking_type'));
        }

        if (request()->filled('ride_type')) {
            $query->where('ride_type', request('ride_type'));
        }

        if (request()->filled('from_date') && request()->filled('to_date')) {
            $query->where(function ($q) {
                $q->whereBetween('dates->from_date', [request('from_date'), request('to_date')])
                    ->orWhereBetween('dates->to_date', [request('from_date'), request('to_date')]);
            });
        } elseif (request()->filled('from_date')) {
            $query->whereDate('dates->from_date', request('from_date'));
        } elseif (request()->filled('to_date')) {
            $query->whereDate('dates->to_date', request('to_date'));
        }

        return $query;
    }


    /**
     * Optional method if you want to use the html builder.
     */
    public function html(): HtmlBuilder
    {
        return $this->builder()
            ->setTableId('booking-table')
            ->setTableAttribute('class', 'table table-striped table-bordered dt-responsive nowrap jambo_table bulk_action')
            ->columns($this->getColumns())
            ->ajax([
                'url' => route('booking.index'),
                'data' => 'function(d) {
                    d.status = $("#status").val();
                    d.ride_type = $("#ride_type_filter").val();
                    d.booking_type = $("#booking_type").val();
                    d.from_date = $("#from_date").val();
                    d.to_date = $("#to_date").val();
                }',
            ])
            ->orderBy(1)
            ->selectStyleSingle()
            ->responsive(true)
            ->autoWidth(false);
    }


    /**
     * Get the dataTable columns definition.
     */
    public function getColumns(): array
    {
        return [
            Column::computed('control')
                ->title('')
                ->exportable(false)
                ->printable(false)
                ->width(30)
                ->addClass('control')
                ->orderable(false)
                ->searchable(false)
                ->data('control')
                ->defaultContent(''),
            // Column::make('DT_RowIndex')->title('S.No')->orderable(false)->searchable(false),
            Column::make('booking_code')->title('Booking ID')->addClass('all'),
            Column::make('ride_type')->title('Ride Type')->addClass('all'),
            Column::make('booking_type')->title('Booking Type'),
            Column::make('total_trips')->title('Total Trips')->addClass('none'),
            Column::make('no_of_passengers')->title('Passenger')->addClass('none'),
            Column::make('status')->title('Status')->addClass('all'),
            // Column::make('created_at')->title('Created At'),
            Column::computed('action')
                ->exportable(false)
                ->printable(false)
                ->width(120)
                ->addClass('text-center all'),
        ];
    }

    /**
     * Get the filename for export.
     */
    protected function filename(): string
    {
        return 'Booking_' . date('YmdHis');
    }
}