Querying translations | laravel-translatable | Spatie

 SPATIE

  Laravel Translatable
=======================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-translatable](https://spatie.be/docs/laravel-translatable/v6)  Basic-usage  Querying translations

 Version   v6

 Other versions for crawler [v6](https://spatie.be/docs/laravel-translatable/v6)

  Querying translations
- [ Introduction ](https://spatie.be/docs/laravel-translatable/v6/introduction)
- [ Support us ](https://spatie.be/docs/laravel-translatable/v6/support-us)
- [ Requirements ](https://spatie.be/docs/laravel-translatable/v6/requirements)
- [ Upgrading ](https://spatie.be/docs/laravel-translatable/v6/upgrading)
- [ Installation &amp; setup ](https://spatie.be/docs/laravel-translatable/v6/installation-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-translatable/v6/questions-issues)
- [ Changelog ](https://spatie.be/docs/laravel-translatable/v6/changelog)
- [ About us ](https://spatie.be/docs/laravel-translatable/v6/about-us)

Basic usage
-----------

- [ Getting and setting translations ](https://spatie.be/docs/laravel-translatable/v6/basic-usage/getting-and-settings-translations)
- [ Removing translations ](https://spatie.be/docs/laravel-translatable/v6/basic-usage/removing-translations)
- [ Replacing translations ](https://spatie.be/docs/laravel-translatable/v6/basic-usage/replacing-translations)
- [ Querying translations ](https://spatie.be/docs/laravel-translatable/v6/basic-usage/querying-translations)
- [ Validating translations ](https://spatie.be/docs/laravel-translatable/v6/basic-usage/validating-translations)
- [ Handling missing translations ](https://spatie.be/docs/laravel-translatable/v6/basic-usage/handling-missing-translations)

Advanced usage
--------------

- [ Available events ](https://spatie.be/docs/laravel-translatable/v6/advanced-usage/available-events)
- [ Customize the toArray method ](https://spatie.be/docs/laravel-translatable/v6/advanced-usage/customize-the-toarray-method)
- [ Usage with factories ](https://spatie.be/docs/laravel-translatable/v6/advanced-usage/usage-with-factories)

 Querying translations
=====================

If you're using MySQL 5.7 or above, it's recommended that you use the JSON data type for housing translations in the db. This will allow you to query these columns like this:

```
NewsItem::where('name->en', 'Name in English')->get();
```

Or if you're using MariaDB 10.2.3 or above :

```
NewsItem::whereRaw("JSON_EXTRACT(name, '$.en') = 'Name in English'")->get();
```

If you want to query records based on locales, you can use the `whereLocale` and `whereLocales` methods.

```
NewsItem::whereLocale('name', 'en')->get(); // Returns all news items with a name in English

NewsItem::whereLocales('name', ['en', 'nl'])->get(); // Returns all news items with a name in English or Dutch
```

If you want to query records based on locale's value, you can use the `whereJsonContainsLocale` and `whereJsonContainsLocales` methods.

```
// Returns all news items that has name in English with value `Name in English`
NewsItem::query()->whereJsonContainsLocale('name', 'en', 'Name in English')->get();

// Returns all news items that has name in English or Dutch with value `Name in English`
NewsItem::query()->whereJsonContainsLocales('name', ['en', 'nl'], 'Name in English')->get();
```

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
