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

How do I get only the "title" column, excluding the "content" column? #343

Closed
ast21 opened this issue Mar 28, 2023 · 4 comments
Closed

How do I get only the "title" column, excluding the "content" column? #343

ast21 opened this issue Mar 28, 2023 · 4 comments
Labels

Comments

@ast21
Copy link

ast21 commented Mar 28, 2023

Good afternoon!

I have a table of articles and article_translations.
article_translations contains two translatable columns title and content. The content column can contain more than 10,000 characters. To get all the articles I do this:

Article::withTranslation()->paginate(50);

and this code get all translated columns, including content, but the content column is not needed, and loads the request

How do I get only the title column, excluding the content column?

@ast21 ast21 added the question label Mar 28, 2023
@github-actions
Copy link
Contributor

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

@github-actions github-actions bot added the stale label Apr 19, 2023
@ast21
Copy link
Author

ast21 commented Apr 19, 2023

hello, any updates and answers?

@github-actions github-actions bot removed the stale label Apr 20, 2023
@omarcinkonis
Copy link

omarcinkonis commented Apr 28, 2023

Hi, I found your question because I had a similar issue. To avoid loading unnecessary columns, you can add disableAutoloadTranslations() to the constructor of your model:

    public function __construct()
    {
        parent::__construct();

        self::disableAutoloadTranslations();
    }

If you want to disable autoload globally, you can change this translatable.php setting to false:
'to_array_always_loads_translations' => false,

Refer to: https://docs.astrotomic.info/laravel-translatable/package/methods#translation-autoloading

@ast21
Copy link
Author

ast21 commented Apr 29, 2023

Hi @omarcinkonis .

Thanks for your reply. Now I use that code:

Article::query()
    ->with('translations', function ($query) {
        $query
            ->select(['article_id', 'locale', 'title']) // without 'content' because it's too big
            ->where('locale', app()->getLocale());
    })
    ->paginate();

After that, I wrap it in ArticleResource, there I already bring it to the form I need.

@ast21 ast21 closed this as completed May 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants