coolsam726/jetstream-inertia-generator

what is index action in controller in API being used for

Closed this issue · 3 comments

what is index action in controller in API being used for?

public function index(IndexStore $request)
{
$query = Store::query(); // You can extend this however you want.
$cols = [
Column::name('id')->title('Id')->sort()->searchable(),
Column::name('location_name')->title('Location Name')->sort()->searchable(),
Column::name('address_1')->title('Address 1')->sort()->searchable(),
Column::name('address_2')->title('Address 2')->sort()->searchable(),
Column::name('city')->title('City')->sort()->searchable(),
Column::name('province')->title('Province')->sort()->searchable(),
Column::name('email')->title('Email')->sort()->searchable(),
Column::name('phone_no')->title('Phone No')->sort()->searchable(),
Column::name('updated_at')->title('Updated At')->sort()->searchable(),

        Column::name('actions')->title('')->raw()
    ];
    $data = Pagetables::of($query)->columns($cols)->make(true);
    return $this->api->success()->message("List of Stores")->payload($data)->send();
}

public function dt(Request $request) {
    $query = Store::query()->select(Store::getModel()->getTable().'.*'); // You can extend this however you want.
    return $this->repo::dt($query);
}
rakoi commented

it returns the default columns for the datatable for the respective models

Hi @abhitheawesomecoder,
Initially it was being used to actually render the Datatables, but since we moved to using Yajra datatables, this index function now is used mainly for fetching paginated lists of models and searching the models. If you check closely you will realize that all the InfiniteSelect input components call this method (e.g route('api.roles.index')) in order to fetch a paginated list of models, while at the same time offering search. The $cols variable sets the fields which can be searched within the model. You can even set relationships using dots. I hope this helps.