Last active
August 29, 2015 14:05
-
-
Save JordanDalton/a120a888e6652e1d47d7 to your computer and use it in GitHub Desktop.
Here is an example of how you can utilize Laravel 4.3 FormRequest to force a unique rule to ignore a given ID
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace App\Http\Requests; | |
use Auth; | |
use Illuminate\Foundation\Http\FormRequest; | |
class UpdateUserRequest extends FormRequest { | |
/** | |
* Get the validation rules that apply to the request. | |
* | |
* @return array | |
*/ | |
public function rules() | |
{ | |
/* | |
* Route::resource('users', 'UsersController'); | |
* PUT users/{users} | |
* PATCH users/{users} | |
* | |
* http://laravel43.dev:8000/users/2 | |
* | |
* Here are a couple of ways to obtain the | |
* identifying value from the URI. | |
* | |
* Method #1: | |
* $this->route->parameter('users') | |
* | |
* Method #2: | |
* $this->segment(2) | |
* | |
*/ | |
$id = $this->route->parameter('users'); | |
return [ | |
'email' => "required|unique:users,email,$id" | |
]; | |
} | |
/** | |
* Determine if the user is authorized to make this request. | |
* | |
* @return bool | |
*/ | |
public function authorize() | |
{ | |
return Auth::id() == $this->route->parameter('users'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment