Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Working with Laravel Datatables #113

Closed
gyan111 opened this issue Dec 24, 2019 · 6 comments
Closed

Working with Laravel Datatables #113

gyan111 opened this issue Dec 24, 2019 · 6 comments
Assignees
Labels

Comments

@gyan111
Copy link

gyan111 commented Dec 24, 2019

I have checked and it seems like it is hard to work if we are using laravel data tables package

Does it work with that package.

Is there any documentation available

@Gummibeer
Copy link
Member

Could you get into more detail what your problem is?
Do you want to display the current locale in one column or all locales in multiple columns?

@gyan111
Copy link
Author

gyan111 commented Dec 24, 2019

I want to display one locale in columns.

I am able to bring the data and the json returns correctly.

There are two issues
#1 While showing data in columns
when I try to show data like this
{'data': 'age', 'name': 'age' } //it works
{'data': 'transaltion[0].first_name, 'name': 'first_name' } //I have tow locals so it show like FirstName_English0FirstName_Otherlocale

#2 While searching or sorting the table. Datatables runs queries which is not compatible for translations.

I know I have represented the questions and sorry about that. I can explain more.

@Gummibeer Gummibeer self-assigned this Dec 24, 2019
@Gummibeer
Copy link
Member

I'm not this deep in the other package. Some ideas you could try to play with:
https://docs.astrotomic.info/laravel-translatable/package/methods#gettranslationsarray
Returns an Array possible better usable for this. But it would require you to use the array/collection data table driver instead of the eloquent/DB one.
I would recommend you to don't use translations[0] because the numerical index isn't a safe accessor.

For sorting you could try https://docs.astrotomic.info/laravel-translatable/package/scopes#orderbytranslation-string-usdtranslationfield-string-usdsortmethod-asc - I bet that the datatable package accepts custom order queries/scopes in any way.

At the end you will have to dig deeper into the datatable package. But I would be fine/happy to accept a PR in the docs to add guides for how to do it.

Except of this:

I wish you and your loved ones a Merry Christmas!
And now go off the PC and have a great time. 😉
🎅🎄🎁🦌⛄❄️🤶🍗🥧🍮🍷🍽️🍫🍬🍭

@gyan111
Copy link
Author

gyan111 commented Dec 24, 2019

Thank you for your suggestios.

Your wish made me smile.

Thank you.

@gyan111 gyan111 closed this as completed Dec 24, 2019
@weidmaster
Copy link

weidmaster commented Aug 20, 2020

I have some issues with datatables as well. When using server-side, we don't use get() in the Eloquent model, so the translations are not loaded at all and the search in a translatable field will not be present in the query, so we need to define the filterColumn method in the datatable.

 $datas = Generalsetting::orderBy('id');
        //--- Integrating This Collection Into Datatables
        return Datatables::of($datas)
            ->filterColumn('title', function ($query, $keyword) {
                $query->whereTranslationLike('title', "%{$keyword}%", $this->lang->locale);
            })
            ->toJson(); //--- Returning Json Data To Client Side

Also, I tried to select just a few data, like id and title, and title is translatable, but using Model::select('id','title') does not work, because the translations table is not loaded at all for the query to work.

That makes queries slower with lots of data. It should be possible to select the fields we want in select with translations, like:

$datas = Generalsetting::select('id','title')->orderBy('id');

I tried to chain the call using withTranslation() but it does not work either and listsTranslation() only accepts one field, so I can't return title and subject for instance in the same call.

EDIT: I forgot to ping you @Gummibeer as it is a closed issue but with new information

@Gummibeer
Copy link
Member

@weidmaster so far I've understand it your issue is that select() doesn't work with translatable data/columns!?
Even if it somehow belongs to this, as the datatables seem to be your usecase, it's a different topic.
Could you please open a new issue?
But to be honest: I won't really work on it - so it will depend on the community.
It's a topic requested since nearly the beginning of this package - but I will won't put much time in it.
Why: in all the time I'm working with Laravel I've really never used select() on an Eloquent query and still had very good query performance. So yes it could be that you have such a big table that a select is useful. But most times my experience is that missing index, wrong column types or putting too much data in one table is the real issue - while the last one happened nearly never.^^

If anyone comes up with a working solution that is maintainable I will happily merge it, but even the PHP memory issue is solvable with LazyCollections right now. 🎉

@Astrotomic Astrotomic locked as resolved and limited conversation to collaborators Aug 28, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

No branches or pull requests

3 participants