|
| 1 | +# Laravel Unique Translation |
| 2 | + |
| 3 | +#### Check if a translated value in a JSON column is unique in the database. |
| 4 | + |
| 5 | +Imagine you want store a `slug` for a `Post` model in different languages. |
| 6 | + |
| 7 | +The amazing [`spatie/laravel-translatable`](https://github.com/spatie/laravel-translatable) package makes this a cinch! |
| 8 | + |
| 9 | +But then you want to make sure each translation is unique for its language. |
| 10 | + |
| 11 | +That's where this package comes in to play. |
| 12 | + |
| 13 | +## Requirements |
| 14 | + |
| 15 | +- PHP >= 7.0 |
| 16 | +- MySQL >= 5.7 |
| 17 | +- [Laravel](https://laravel.com/) >= 5.5 |
| 18 | +- [spatie/laravel-translatable](https://github.com/spatie/laravel-translatable) |
| 19 | + |
| 20 | +## Installation |
| 21 | + |
| 22 | +Require the package via Composer: |
| 23 | + |
| 24 | +``` |
| 25 | +composer require codezero/laravel-unique-translation |
| 26 | +``` |
| 27 | +Laravel will automatically register the [ServiceProvider](https://github.com/codezero-be/laravel-unique-translation/blob/master/src/UniqueTranslationServiceProvider.php). |
| 28 | + |
| 29 | +## Usage |
| 30 | + |
| 31 | +For the following examples, I will use a `slug` in a `posts` table as the subject of our validation. |
| 32 | + |
| 33 | +### Validate a Single Translation |
| 34 | + |
| 35 | +Your form can submit a single slug: |
| 36 | + |
| 37 | +```html |
| 38 | +<input name="slug"> |
| 39 | +``` |
| 40 | + |
| 41 | +We can then check if it is unique **in the current locale**: |
| 42 | + |
| 43 | +```php |
| 44 | +$attributes = request()->validate([ |
| 45 | + 'slug' => ['unique_translation:posts'], |
| 46 | +]); |
| 47 | +``` |
| 48 | + |
| 49 | +You could also use the Rule instance: |
| 50 | + |
| 51 | +```php |
| 52 | +use CodeZero\UniqueTranslation\UniqueTranslationRule; |
| 53 | + |
| 54 | +$attributes = request()->validate([ |
| 55 | + 'slug' => [new UniqueTranslationRule('posts')], |
| 56 | +]); |
| 57 | +``` |
| 58 | + |
| 59 | +### Validate an Array of Translations |
| 60 | + |
| 61 | +Your form can also submit an array of slugs. |
| 62 | + |
| 63 | +```html |
| 64 | +<input name="slug[en]"> |
| 65 | +<input name="slug[nl]"> |
| 66 | +``` |
| 67 | + |
| 68 | +We need to validate the entire array in this case. Mind the `slug.*` key. |
| 69 | + |
| 70 | +```php |
| 71 | +$attributes = request()->validate([ |
| 72 | + 'slug.*' => ['unique_translation:posts'], |
| 73 | + // or... |
| 74 | + 'slug.*' => [new UniqueTranslationRule('posts')], |
| 75 | +]); |
| 76 | +``` |
| 77 | + |
| 78 | +### Specify a Column |
| 79 | + |
| 80 | +Maybe your form field has a name of `post_slug` and your database field `slug`: |
| 81 | + |
| 82 | +```php |
| 83 | +$attributes = request()->validate([ |
| 84 | + 'post_slug.*' => ['unique_translation:posts,slug'], |
| 85 | + // or... |
| 86 | + 'post_slug.*' => [new UniqueTranslationRule('posts', 'slug')], |
| 87 | +]); |
| 88 | +``` |
| 89 | + |
| 90 | +### Ignore a Record with ID |
| 91 | + |
| 92 | +If you're updating a record, you may want to ignore the post itself from the unique check. |
| 93 | + |
| 94 | +```php |
| 95 | +$attributes = request()->validate([ |
| 96 | + 'slug.*' => ["unique_translation:posts,slug,{$post->id}"], |
| 97 | + // or... |
| 98 | + 'slug.*' => [(new UniqueTranslationRule('posts'))->ignore($post->id)], |
| 99 | +]); |
| 100 | +``` |
| 101 | + |
| 102 | +### Ignore Records with a Specific Column and Value |
| 103 | + |
| 104 | +If your ID column has a different name, or you just want to use another column: |
| 105 | + |
| 106 | +```php |
| 107 | +$attributes = request()->validate([ |
| 108 | + 'slug.*' => ['unique_translation:posts,slug,ignore_value,ignore_column'], |
| 109 | + // or... |
| 110 | + 'slug.*' => [(new UniqueTranslationRule('posts'))->ignore('ignore_value', 'ignore_column')], |
| 111 | +]); |
| 112 | +``` |
| 113 | + |
| 114 | +## Example |
| 115 | + |
| 116 | +Your existing `slug` column (JSON) in a `posts` table: |
| 117 | + |
| 118 | +```json |
| 119 | +{ |
| 120 | + "en":"not-abc", |
| 121 | + "nl":"abc" |
| 122 | +} |
| 123 | +``` |
| 124 | + |
| 125 | +Your form input to create a new record: |
| 126 | + |
| 127 | + |
| 128 | +```html |
| 129 | +<input name="slug[en]" value="abc"> |
| 130 | +<input name="slug[nl]" value="abc"> |
| 131 | +``` |
| 132 | + |
| 133 | +Your validation logic: |
| 134 | + |
| 135 | +```php |
| 136 | +$attributes = request()->validate([ |
| 137 | + 'slug.*' => ['unique_translation:posts'], |
| 138 | +]); |
| 139 | +``` |
| 140 | + |
| 141 | +The result is that `slug[en]` is valid, since the only `en` value in the database is `not-abc`. |
| 142 | + |
| 143 | +And `slug[nl]` would fail, because there already is a `nl` value of `abc`. |
| 144 | + |
| 145 | + |
| 146 | +## Testing |
| 147 | + |
| 148 | +``` |
| 149 | +vendor/bin/phpunit |
| 150 | +``` |
| 151 | + |
| 152 | +## Security |
| 153 | + |
| 154 | +If you discover any security related issues, please [e-mail me](mailto:ivan@codezero.be) instead of using the issue tracker. |
| 155 | + |
| 156 | +## License |
| 157 | + |
| 158 | +The MIT License (MIT). Please see [License File](https://github.com/codezero-be/laravel-unique-translation/blob/master/LICENSE.md) for more information. |
0 commit comments