mercredi 30 mars 2016

Validation rule to check against multiple rows

When updating location names, I need validation to check against multiple rows in the table, not just a single id.

My table looks like this:

+----+--------------+-----------+----------+
| id |     name     |    lat    |   lng    |
+----+--------------+-----------+----------+
|  1 | Location One | 53.348333 | 0.348333 |
|  2 | Location One | 57.348222 | 0.348222 |
|  3 | Location One | 57.348111 | 0.545454 |
|  4 | Location Two | 55.348554 | 0.555444 |
|  5 | Location Two | 56.348667 | 0.348333 |
|  6 | Location Two | 56.348778 | 0.111111 |
+----+--------------+-----------+----------+

Creating new locations works as expected. But I'm not sure how to set the validation rule to exclude the current location I'm trying to update - it needs to check against the 'name' column.

I was hoping something like this may work - but it doesn't.

public function rules()
{
    return [
        'name' => 'required|max:255|unique:evac_routes,name,'.$this->name,
         ...
    ];
}

In my controller I'm using Route::where('name', $route->name)->update, which works, but I can't convert this logic to the validation rule:

public function update($id, UpdateRouteRequest $request)
    {

        $route = Route::findOrFail($id);    

        $updateRows = Route::where('name', $route->name)->update([
            'name' => $request->name,
            ...
            ]);            

        return redirect('routes');    

    }



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1q1Oaal
via IFTTT

Aucun commentaire:

Enregistrer un commentaire