October 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 April 29th, 2021

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

1 comment

0
alex
Post on June 28th, 2020 7:16 AM

Hi, how do you add this code? On the blogposts partial? on the page where there will be displayed? Merci!

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