Laravel Form Requests Involving Record Updates with a Unique Field

  • April 26, 2016

Welcome to the incredibly popular Easy Laravel 5 companion blog. To celebrate the new edition's release (updated for Laravel 5.5!) use the discount code easteregg to receive 20% off the book or book/video package! » Buy the book

Web applications are often tasked with ensuring certain database record fields contain unique values, and frameworks such as Laravel greatly reduce the amount of logic required to add this capability by offering

public function rules()
{
    return [
        'name'           => 'required|string',
        'email'          => 'required|email',
        'tax_id'         => 'required|string|size:10unique:employers,tax_id,' . \Auth::id(),
    ];

}

If you're not familiar with Laravel 5's form requests feature, then I suggest reading this tutorial first.