How to implement Server-Side Datatables using CodeIgniter and Eloquent ORM - codeigniter

I have implemented Eloquent ORM using CodeIgniter and been able to fetch records from the database.
Need some help with implementing Server-side datatables using the above.
http://datatables.net/examples/data_sources/server_side.html
Tried using this:
$data = User::all()->toJson();
Gets data in JSON but doesnt work as it needs more data for datatable like the number of records and number of times table is drawn etc.
I am really stuck out and this small fix would help me a lot.

After various efforts instead of fetching data using ORM used Ignited Datatables library and pushed data to datatables. For now helped me. But would surely find some alternative by using the illuminate database.

Related

Dexie to query JavaScript arrays

I am working on an application in angular that is making great use of IndexedDb via dexie. I am in love with dexie due to its support to query indexed db stores in a sql like way. My question is can we use WhereClause of dexie with simple javascript arrays along with indexeddb stores? What i meant is
[{Id:1,test:"ABC"},{Id:2,test:"XYZ"}].where("test").equals('ABC').toArray()

What are the best options for data loading in bigger systems? (Laravel)

as my question says, I would like to know what is my best choice for loading data in bigger systems in Laravel.
At the moment I use Laravel Eloquent to pull data from database and in my views I use dataTables JS library. It is effective for smaller systems or websites. However I seem to find myself in a position where systems that are bigger and more complex take very long time to load those data (sometimes more than 15 seconds).
I have found some solutions:
Eager loading relations helps with relations in row
Using DB instead of Eloquent
using Laravel pagination instead of dataTables pagination
loading data to dataTables from ajax source
However the pagination has some problems, especially with dataTables having the option of ordering/searching data.
My question is do you have any advice on how to load data in most effective way, so that it is as fast as it could be and the code as clean as possible. What do you do, when you need to load ginormous amount of data?
On of the best ways to optimize your queries is by using MySQL indexes. As per this documentation:
Without an index, MySQL must begin with the first row and then read
through the entire table to find the relevant rows. The larger the
table, the more this costs. If the table has an index for the columns
in question, MySQL can quickly determine the position to seek to in
the middle of the data file without having to look at all the data.
This is much faster than reading every row sequentially.
The most simple way to create an index is the following:
CREATE INDEX index_name
ON table_name (column1, column2, ...);
If you want the Laravel way of creating index, you can do it by using index() method, as per official documentation:
$table->index('column');

Repeatable Data in Laravel

I'm creating a Laravel app but come from a WordPress background. In WordPress, it's reasonably straight forward to create custom fields that are repeatable e.g. I have a field called "Task" that can be repeated X number of times and it will be stored on the database.
Is there a best practice way of doing this in Laravel?
I understand that Javascript can be used to create repeatable form fields, and I could store that data as JSON in a MySQL database (using the latest versions of MySQL), but I'd also like this repeatable data to hold relationships e.g. relate a task to a day of the week (stored in another table).
Any advice or thoughts are much appreciated.

Laravel limit records with pagination

I'm using Laravel 5.2 and when working with pagination, I cannot limit the result. When I try with $query->take($x), the result is x records, but when I try with $query->take($x)->paginate(5), it give me all records with 5 records per page.
Could anyone work with laravel pagination could give a hint to resolve that problem?
Thank you so much!
It is not about just fetching the records, you will have to add a number of features in the grid e.g. may be export, searching, filtration etc. I have used Nayjest grid and would recommend you to use another one. I came across and issue and contacted the community and that's what is their reply:
At all, I would recommend to migrate to next major version of grids that is moved to separate repository: https://github.com/view-components/grids
Tip: If you have a lot of usage of nayjest/grids, you can use view-components/grids in parallel for your new reports.
so you should go for:
https://github.com/view-components/grids

Laravel: Models vs DB Queries

I have multiple migrations (say around 10) and the corresponding models for the tables. The problem I am facing is that all the migrations/tables have multiple primary keys but the model has just a string variable to define primary key ($primaryKey), hence, when I save or update a table row using where clause it would just take one primary key and miss the other and hence end up changing multiple rows instead of one. So, basically I switched to DB Queries and it worked well.
So, my question to you is that is there any performance gain with model? Or it is just a designing paradigm? Is there any option to do the same thing (Have multiple primary key) within a model? I know by overriding the Eloquent methods we can do this but is there any other good option?
Using an ORM like Eloquent is a convenience, not a requirement. From what I've heard there's actually often a slight performance decrease when using an ORM because there's the additional overhead of translating that query into the relevant SQL.
Often, using an ORM makes for queries that are much easier to understand, and for that reason alone it's worth using. However, for more complex queries, an ORM is likely to get in the way, and so you should consider using another method to query the database. You don't have to use just an ORM - you can mix and match the different methods as you see fit.
Laravel has the Query Builder as an alternative to writing raw SQL or using the ORM, and that may be a better option for you here. I would avoid writing raw SQL if you can because both Eloquent and the Query Builder will handle escaping the parameters for you, to help avoid SQL injection vulnerabilities.
My choice would be to use the ORM where possible, and fall back to the Query Builder when the ORM gets in the way.

Resources