Winter CMS resources and help articles

Simple and to the point. Optimized by the community.

Find all Blog Posts that have a translation in a specific locale

1
by mjauvin, last modified on July 23rd, 2021 - Previously published on OctoberTricks

If you need to find all Blog Posts that have a translation in a specific locale, use this:

$locale = 'fr';
$posts =  RainLab\Blog\Models\Post::whereHas('translations', function ($query) use ($locale) {
    $query->where('locale', $locale)->whereRaw("attribute_data not like '%\"content\":\"\"%'");
})->get();

Note: this obviously does not work for the DEFAULT locale

This can easily be integrated with the blogPosts component by adding this in your PHP code section:

function onStart()
{
    $locale = $this->activeLocale;
    if ($locale === 'fr') {
        $untranslatedPostIds = RainLab\Blog\Models\Post::whereDoesntHave('translations', function ($query) use ($locale) { 
            $query->where('locale', $locale)->whereRaw("attribute_data not like '%\"content\":\"\"%'");
        })->lists('id');
        $this->blogPosts->setProperty('exceptPost', $untranslatedPostIds);
    }
}

Discussion

0 comments

We use cookies to measure the performance of this website. Do you want to accept these cookies?