How to create custom validation rule for 4 combinedly unique columns

Since Laravel’s default unique:tablename validation rule allows only two columns; in scenarios where more than two columns are unique, it directly throws the QueryException with SQLSTATE errorcode 23000. As a temporary workaround I am catching the exception and throwing a warning like this:

try{
    //My query that might throw an exception for a duplicate entry
} catch (Exception $e){
    return $e->getCode() == 23000 ?
           redirect()->back()->with('warning','Duplicate entry') :
           redirect()->back()->with('error','Unknown Error Occurred');
}

The problem is, for every other scenarios where the errorcode is not 23000, I will only see an error message Unknown Error Occurred instead of Whoops. Clearly, debugging will be a nightmare.

So I looked for alternatives. I had to pick one of two options: a) Use a third party package or, b) Create a custom validation rule

I chose option b. But I am struggling with how I can construct one myself. When I looked at an example (for one-time inline rule):

$validator = Validator::make($request->all(), [
    'title' => [
       'required',
       'max:255',
        function($attribute, $value, $fail) {
            if ($value === 'foo') {
                return $fail($attribute.' is invalid.');
            }
        },
    ],
]);

…I realized that the rule can be applied to a single attribute and value only. But I have 4 attributes and values to check if they are indeed a unique entry. How can I do that?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I use like this,

public function store(Request $req)
{
    $column1 = $req->input('column1');
    $column2 = $req->input('column2');
    $column3 = $req->input('column3');
    $column4 = $req->input('column4');


    $whereData = [
        ['column1', $column1],
        ['column2', $column2],
        ['column3', $column3],
        ['column4', $column4]
    ];

    $count = DB::table('yourtablename')->where($whereData)
                                ->count();

    if($count > 0){
        // The combined is not unique
        //send error message
    }else{
        //do whatever u need
    }

}

You may implement this in AppServiceProvider‘s boot method also.

Add this code to your boot method:

Validator::extend('uniqueofFourColumns', function ($attribute, $value, $parameters, $validator) {

    $whereData = [
        ['column1', $value],
        ['column2', $parameters[0]],
        ['column3', $parameters[1]],
        ['column4', $parameters[2]]
    ];

    $count = DB::table('yourtablename')->where($whereData)->count();

    return $count === 0;
});

Then u may use uniqueofFourColumns this rule wherever u need:
'column1' => 'uniqueofFourColumns:'.{{$request->column2}}.', '.{{$request->column3}}.', '.{{$request->column4}}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x