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
Translations are not loaded with Datatables in server-side mode #171
Comments
Hey, thanks for clarifying the real issue. I don't see a good solution that could be added to this package. If there's more you could imagine in a translatable-datatable bridge I would recommend a dedicated package. For sure I will list it in the docs - I'm thinking/planning something similar for Nova. The dedicated class would bee something similar like the repository pattern. In theory you could even add a |
I understand that the package needs to be agnostic as possible. Regarding your statement:
I think then it only needs to provide a new method that can be chainable, instead of relying to using As for the wrapper idea, I don't know how to do it, but I understand the concept of having a base class that will handle the interaction. I just don't understand how I will be able to wrap the package in my code. Thanks as always. |
Hey, The wrapper, as query builder, could be something like this - it's not tested and more pseudo as I'm writing it on my phone. 😅 class PostQueryBuilder extends Eloquent\QueryBuilder
{
public function datatable()
{
return Datatable::of($this)->filter column('title', ...);
}
} After registering it on your model via Post::wherePublished(true)->datatable()->toJson();
Post::datatable(); This way you can build your query as normal, instead of |
Oh I hope you are enjoying your holidays! And thanks for the info, it looks very interesting indeed. I can see a lot of potential. I just never heard about custom query builder. I will need to do some research, but if it works like you explained, that would help a lot. |
<?php
namespace App\Builder\Eloquent;
use App\Enums\FileStatusEnum;
use App\Enums\FileTypeEnum;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Carbon;
class FileQueryBuilder extends Builder
{
public function wherePublished(): self
{
return $this->where('status', FileStatusEnum::PUBLISHED());
}
public function whereImages(): self
{
return $this->where('type', FileTypeEnum::IMAGE());
}
public function wherePublishedImages(): self
{
return $this->wherePublished()->whereImages();
}
public function whereStale(): self
{
return $this->whereIn('status', [
FileStatusEnum::DENIED(),
FileStatusEnum::CLOUD_UPLOAD(),
FileStatusEnum::CREATED(),
])->where('created_at', '<', Carbon::now()->subDay());
}
} That's one of our builders. We use them as soon as some scopes come together on one model to keep the models clean. |
This looks awesome! And I see another cool stuff, the enum type there. That is something I have to catch up on as well. |
If you do use directly the v2 dev - it will be released soon but we need some real world feedback. 😉 |
Oh so you contribute to the spatie codebase. That is pretty awesome. I will take a look. I saw it is for L8 only though, so it may take a while. |
Yeah, several packages - most common the activitylog which I maintain. Yeah, the |
This issue is stale because it has been open 21 days with no activity. Remove stale label or comment or this will be closed in 7 days |
Picking up from #113 the problem is not with the
select()
at all, it is with the datatable when using server-side mode.When using server-side, we don't get the results from Eloquent, we let the datatable package make the queries and assemble the final result to send to the client-side, and that is why the translations are not loaded with the request.
The following code will be the example:
The filterColumn is needed because without it when performing a search in the datatable, it would try to search the vanilla field of title. So the problem is just that we need to copy such filter logic in every table ^^'
One approach I can see is for Translatable package to notice that the model (Generalsetting in the example) does use the trait, or have the
$translatedAttributes
array, and modify the query to join the translated table so the field would be available in the query.Other approach would be for
withTranslation()
to make the query without using get() as well, thus making the query available.Another way that I can see this working is for the method
listsTranslations()
to accept an array of fields and return the query as well, so instead of doing like:Generalsetting::listsTranslations('title')->get()
it would be just `Generalsetting::listsTranslations('title')' and we could chain the order and other eloquent calls, without getting the results right away, so the effect would be to only return the fields we care about in the particular table.I guess the bottom line is in the fact that every method needs to call get() and can't be used in chain with other Eloquent methods.
The text was updated successfully, but these errors were encountered: