How to set unique for specific field from specific table? - laravel

I have this where i have all features from database:
$features = Feature::all();
User can add new additional feature that will be added in this table, but i want to validate so if user enter something that is already in databse to get a message. So name need to be unique. Any suggestion how can i do that?
I tried this but it save it anyway.
$this->validate($request, [
'name' => 'unique:features',
]);

From the docs
unique:table,column,except,idColumn
The field under validation must be unique in a given database table. If the column option is not specified, the field name will be used.
Specifying A Custom Column Name:
'email' => 'unique:users,email_address'
You may need to specify the column to be checked against.

$feauturescheck= Feauture::where('Columname', '=',Input::get('input'))->count();

Related

how to override index query during runtime and update the table laravel-nova

in my use case, I need more than one text box for searching in more than two fields in the index view at the same time, because there too much data, laravel-nova provide only one out of the box, so if there is a way that I can add search in a card and update the index query in run time it will be great, and if there are any other solutions for this problem I would appreciate it.
Laravel Migration:
Create the migration files to set up index queries.
In the function, wherever you needed, use the below command to run the migration.
Artisan::call('migrate', array('--path' => 'app/migrations', '--force' => true));
Here, you can also specify the migration file name like,
Artisan::call('migrate', array('--path' => 'app/migrations/create_index.php', '--force' => true));

best way for bulk insertion validation laravel 5

i have problem validating a bulk insertion in laravel 5 the scenario that i have is as following :
a model called department that has many employees when i save a department it may have several employee belongs to it , I'm currently loops the entire employee list and validate each one before insertion is there a way in laravel 5 validator that do this out of the box.
In Laravel 5.2 there's an easy way of taking care of this kind of problems.
Validating array form input fields is much easier in Laravel 5.2. For example, to validate that each e-mail in a given array input field is unique, you may do the following:
$validator = Validator::make($request->all(), [
'person.*.email' => 'email|unique:users'
]);
Likewise, you may use the * character when specifying your validation messages in your language files, making it a breeze to use a single validation message for array based fields:
'custom' => [
'person.*.email' => [
'unique' => 'Each person must have a unique e-mail address',
]
],
This info is clearly explained in the docs.
Source: laravel.com
To my knowledge there is not a way to do this in Laravel out of the box. By that I mean there is no method you can call or class you can use that will accomplish your goal.
You can create one though. Abstract the validation out into it's own class and use that.

Updating a model with a unique column in laravel 5

I've tried following this question, which seems like what I want, but have had no luck.
I have a Page model which has an incrementing id, a title, a slug (unique), and an html column. I want to be able to change the title, slug, and html fields. I've tried adding the rule:
'slug' => 'required|unique:pages,slug,{$this->slug}',
for my PATCH request, but when I submit my form (with the same slug), I get an error that it's already taken. If I change the slug, the title and html changes, but the slug does not.
Ideally I'd rather have the slug be generated automatically and update in the background, but I've not been able to get that to work out either.
How can I allow for a changing unique field when updating a model, or generate it automatically?
You need to provide the unique rule with three arguments to add an exception.
The Name of the Table that contains the exception (pages)
The Column Name of the exception (slug)
Primary Key of the row that contains the exception. (id)
In your case the primary key is the id field not the slug field.
'slug' => 'required|unique:pages,slug,' . $this->id

how to allow text attributes to be empty in Yii model?

I have a model for articles, the article can be written in two languages, English and Arabic.
So there are 2 attributes for each language text, I want the both attributes to be allowed to be empty on create and update, knowing that the attributes type are text not varchar.
How i can write a rule for what i need?
I am not sure what you mean by "..knowing that the attributes type are text not varchar..." but Yii has built in mechanism to define your validation rules.
Refer to the Yii documentation for more details.
#hamed pointed out in his answer how to make fields mandatory. You can add to this by specifying the scenarios under which the validation rules are true. For example.
array('field1, field2', 'required', 'on' => array('create', 'update'));
will apply the 'required' rule only for inserting and updating. On the other hand.
array('field1, field2', 'required', 'except' => array('create', 'update'));
will apply the 'required' rule only for all scenarios except inserting and updating.
http://www.yiiframework.com/doc/api/1.1/CStringValidator
array(
array('text_en','length'),
array('text_ar','length'),
);
When you generate a model from a database table, yii adds something like this inside rule() function:
array('field1, field2, ...', 'required')
This array is based on NOT NULL attribute in the database column. It means, if the database field has NOT NULL property checked, yiis add it in the above array. All you need to do is removing your two fields from above array.

Laravel 4 - how to use a unique validation rule / unique columns with soft deletes?

Assume, you are trying to create a new user, with a User model ( using soft deletes ) having a unique rule for it's email address, but there exists a trashed user within the database.
When trying to validate the new user's data, you will get a validation error, because of the existing email.
I made some kind of extra validation within my Controllers, but wouldn't it be nice to have it all within the Model?
Would you suggest creating a custom validation rule?
As I haven't found a clean solution now, I am interessted in how others solved this problem.
You can validate passing extra conditions:
'unique:users,deleted_at,NULL'
This sounds like an issue with your business logic rather than a technical problem.
The purpose of a soft delete is to allow for the possibility that the soft-deleted record may be restored in the future. However, if your application needs uniqueness of email (which is completely normal), you wouldn't want to both create a new user with that email address and be able to restore the old one as this would contravene the uniqueness requirement.
So if there is a soft deleted record with the email address for which you are adding as a new record, you should consider instead restoring the original record and applying the new information to it as an update, rather than trying to circumvent the uniqueness check to create a new record.
This is the best approach
'email' => 'required|email|unique:users,email,NULL,id,deleted_at,NULL',
It give you this query
select count(*) as aggregate from `users` where `email` = ? and `deleted_at` is null
Laravel offers "Additional Where Clauses".
My url validation rule (from the update model method) looks like this:
$rules['url'] = 'required|unique:pages,url,'.$page->id.',id,deleted_at,NULL';
This means that the url must be unique, must ignore the current page and ignore the pages where deleted_at id not NULL.
Hope this helps.
Your Eloquent model should have the $softDeletes property set. If so, then when you perform a WHERE check, like User::where('username', 'jimbob'), Eloquent will automatically add in the query WHERE deleted_at IS NULL... which excludes soft deleted items.
In laravel 6.2+ you can use
'email' => ['required', Rule::unique('users')->whereNull('deleted_at')]

Resources