0

I have been trying unique validation in laravel 5.7. Below is my code for validation.

$this->validate($request, [
        'name' => 'required|unique:permissions,name',
        'slug' => 'required|unique:permissions,slug', 
    ]);

And the html is below:

<div class='container'> 
{!! Form::open(array('route' => 'permission.save','method'=>'POST')) !!}
<div class="row">
    <div class="col-xs-12 col-sm-12 col-md-12">
        <div class="form-group">
            <strong>Name:</strong>
            {!! Form::text('name', null, array('placeholder' => 'Name','class' => 'form-control')) !!}
        </div>
        <div class="form-group">
            <strong>Slug:</strong>
            {!! Form::text('slug', null, array('placeholder' => 'Slug','class' => 'form-control')) !!}
        </div>
        <div class="form-group">
            <strong>Description:</strong>
            {!! Form::textarea('description', null, array('placeholder' => 'Description','class' => 'form-control')) !!}
        </div>
    </div> 
    <div class="col-xs-12 col-sm-12 col-md-12 text-center">
        <button type="submit" class="btn btn-primary">Submit</button>
    </div>
</div>
{!! Form::close() !!}
</div>

Unique validation for 'name' is working fine but it is not working for 'slug'. It's really weird i can't get it. Please provide suggestions to fix this issue. Any help would be great.

10
  • 2
    Code looks fine to me. What do you mean by “it’s not working”? Commented Sep 7, 2018 at 12:45
  • Provide html also. Commented Sep 7, 2018 at 12:46
  • @MartinBean unique is not working for 'slug'. But required is working. Its strang Commented Sep 7, 2018 at 12:51
  • can you please provide HTML that you are trying and error message as well? Commented Sep 7, 2018 at 12:52
  • @RamChander I have edited the post and added the html. Commented Sep 7, 2018 at 12:54

1 Answer 1

1

You need to specify column name from database table in unique validation rule

https://laravel.com/docs/5.6/validation#rule-unique

for example your column name for slug is : column_slug

$this->validate($request, [
    'name' => 'required|unique:permissions,name',
    'slug' => 'required|unique:permissions,column_slug',   // column_slug  may be different in your case
]);

And ensure that you have set unique key for that column in database table

Hope it helps !

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.