2
Php

Laravel Tips – Generate Tablular Data With Actions

It’s a very common task in any web application to make a listing of models/records with actions related with them. Probably it’s not clear what I mean. Let’s see it below:

table

Hope it’s clear now. So, it’s a common task in any web application with a backend where an admin can add, edit or delete entries. Most often we need to do the same thing to generate a table like this and it’s realize it’s kind of annoying and repetitive so I was thinking if I could do this with any method call or helper function every time I need to generate something like this again and again. So, finally I’ve come up with a solution to automate this task a little bit. For example to generate the output that is given above as an image, all I need to do is:

In my controller (For Example)

In my view

Here the $data is an instance of Illuminate\Pagination\Paginator and it could be a Collection object as well if I call get() instead of paginate(10) and in this case I need to change the code in my view as given below:

That’s it. I’ll get a table with records and action buttons associated with them very easily. So, what I did is that, I’ve just created a helper function named toTable and used it in my view. The function is given below:

Probably I could have created a package or maybe I could have extended the Collection class but I think it’s not something so big for creating a package and a custom helper function is easy to integrate and modify if needed at any time. Anyways, so it helps me to easily generate tabular data with action buttons but for action buttons I’ve used separate views and in the function above, I’ve just used following code:

It’s obvious that I’ve used a separate Blade View for generating the action buttons and one that I’ve used for this example is as given below:

This view generates button groups and I can use a different set of buttons as well. So, anyways, lets look into the options that could be used with this function. The first parameter in the toTable function is an array of records and the second one is an array of options which is optional. So I can call the function like toTable($data->toArray()) and by default class='table table-condensed' is used to generate a Bootstrap-3 table and all the fields/columns will be dumped in the generated table. So, by providing some options I can customize the output and these are:

→ table (boolean) : Either true or false; for generating only a tbody without a parent table tag.
→ header (boolean) : Either true or false; for generating rows with or without header.
→ only (array) : For specifying the fields/columns to be available in the table.
→ attributes (array) : For using any attributes, i.e. class or id in the table tag.
→ action (string) : The name/path of the view file to be used to generate action buttons.

I’m keeping this function in a helper file in app/start/helpers.php file and including it in global.php file but it’s also possible to use a service provider to include the file. Probably there are other ways to minimize the task (generating a table with action buttons) but it helps me a lot to do it easily. Share your idea if you have something better than this. Thanks!

Latest Blog