Search code examples
jsonlaravelvalidationuniqueexists

Laravel 5.7 exists/unique validation with JSON field type


I have a "countries" table with the following fields:

id: integer
name: json

Field "name" stores value as:

{ "en": "Germany", "de": "Deutschland" }

I wrote the following rule:

'country' => 'nullable|string|max:255|exists:countries,name->en'

but it doesn't work like that. How do I make it work?

MariaDB 10.1.36 / Laravel 5.7


Solution

  • Laravel 5.7 doesn't support JSON queries on MariaDB. This will be fixed in Laravel 5.8.

    In the meantime, you can use this package: https://github.com/ybr-nx/laravel-mariadb