php - How to prevent duplicate values being posted?
Solution:
If you're using Laravel's validation, you can use to ensure uniqueness over all submitted locations:
$request->validate([
'*.location' => 'distinct',
]);
Answer
Solution:
I suggest you the following to have a good quality software :
Create a separate request Validation object called ModelStore with the following command :
php artisan make:request ModelStore
use this created as a parameter of your controller store function. like following
public function store(ModelStore $request)
in this ModelStore class implement 2 more function (there is 2 created by default for error code and message)
public function rules()
{
return [
'request_field' => ['LaravelValidation|anotherLaravelValidation']
];
}
//In this function you can put all your if else validations and returned a validation array
public function all($keys = null)
{
$data = parent::all($keys);
$data['one_field'] = affectValue;
return $data;
}
finaly in your controller store function start with this line to have the data array
$validated_data = $request->validated();
And then you have your data validated and cleaned. in the controller you keep only the logic and not validation.
To answer your initial question : if you want to have a complete unique slug field than you simply implement it in the rules with the distinct or unique laravel validation.
Good luck
Answer
Solution:
when you creating migration set table field as unique
$table->unique('email');
then you searching use distinct
for query
return DB::table('teat_table')
->select('teat_table.*')
->distinct()
->get();
Source