LesPerras.com

Datatables in Laravel

When I installed this implementation of Laravel, I used a tutorial. It introduced me to Data-tables with jquery. I love them. I decided they would be great for a site I have.

The Datatables are free except for the editor function. I downloaded the free part as a quick test and tried them out on my site. They worked great. This relates to my experience setting up an MVC framework from scratch in My First MVC. Despite initial difficulties, I learned that understanding the underlying concepts can make all the difference. Similarly, with Datatables, having a clear understanding of how the buttons work was crucial for a smooth setup. So far, so good.

Editor

I decided to try them with the editor. In fact, I loved Datatables so much that I did not deliberate on the point very long. I thought about it for a good … well fifteen minutes or so before I reached for the credit card. Then I was in downloading heaven.

Installation

As I discussed in Laravel: Adding Markdown Easily, sometimes the documentation can make things look easier than they actually are. In my case, I struggled with implementing Datatables' editor function despite having a good understanding of the concepts. It wasn't until I dug deeper and found additional resources that I was able to overcome the challenges. I did what they said, and then… error. Search. Implement… error. After several iterations of this, (As I discussed in My First MVC, even those with limited experience can learn and overcome difficulties.),I decided to copy and paste the code from their source in the working examples.

It worked! Well, sort of. My routes were set up, and the methods, returning json. But I kept getting errors. I decided on Datatables partly becuase However, this proved not to be the case. Related to what I discussed in My First MVC, it's clear that even with a framework like Laravel, there is still much to learn and explore. This is something I wrote about in Smart Pills - Laravel. As a developer, it's essential to acknowledge when we don't know something and be willing to learn and explore new concepts. In my case, I realized that even with a framework like Laravel, there is still much to learn.

Packages

I went sniffing around https://github.com/yajra/laravel-datatables to see what I could find. This package seemd like an adaptation for the server-side processing which I don’t need, and I could not find any useful links to information on how to use this package for the datatables editor. I decided I could find out myself and learn more, so I left this package although it looked good for server-side processing. Very nice, in fact.

Learning how to debug ajax.

I started to learn all sorts of things, like where to find errors in the chrome developer’s tools, and how to read the returned ajax responses. I have become somewhat of a master at this now. (recently I found out about postman and that would have helped a lot).

Restful responses

I dug deeper when the responses I thought should work did not. I found a page ClientServer Data that told me what I should have looked for at the start. Now I had something concrete to work on. I knew what I needed to output. Next, I had to find a way to get it that way.

{
"data": [
{
"DT_RowId": "row_29",
"first_name": "Fiona",
"last_name": "Green",
"position": "Chief Operating Officer (COO)",
"office": "San Francisco",
"extn": "2947",
"salary": "850000",
"start_date": "2010-03-11"
}]
}
so the part that I was missing was the :

{
"data": [
and the :

 ]
}

at the end. I tried a simple

$var = '{ "data":['.$var.']}';

but I got array to string conversion or object to string conversion errors. Finally, I used the ->toJson () for the collections, and json_encode($var) ; in one instance where I had a stdClass object.

Learning!!

Phew! It was hard but fun and rewarding. I feel like a really learned a lot. Now I want to go back and take on a node based system with socket.io again. Sometime.

Get My Free E-book

Cleaner Living: Breathe through life's difficulties

Learn the scanning breath technique that helped me dissolve decades of emotional baggage. Discover practical tools you can use anywhere to process difficult emotions and find lasting well-being.

We respect your privacy. Your email will only be used to send the download link.