Last active
May 4, 2017 01:39
-
-
Save kikoseijo/2af113852b77f18e71d6f4ec090f4580 to your computer and use it in GitHub Desktop.
Laravel unique rules on FormRequest
This file contains hidden or 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 Illuminate\Foundation\Http\FormRequest; | |
use App\Models\Vehicle; | |
class VehicleRequest extends FormRequest | |
{ | |
/** | |
* Determine if the user is authorized to make this request. | |
* | |
* @return bool | |
*/ | |
public function authorize() | |
{ | |
return true; | |
} | |
/** | |
* Get the validation rules that apply to the request. | |
* | |
* @return array | |
*/ | |
public function rules() | |
{ | |
$reglas = []; | |
switch($this->method()) | |
{ | |
case 'GET': | |
case 'DELETE': | |
{ | |
return []; | |
} | |
case 'POST': | |
{ | |
$reglas = [ | |
'tag' => 'required|unique:vehicles,tag', | |
'vehicle_status_id' => 'required|exists:vehicle_status,id', | |
'item_id' => 'required|exists:items,id', | |
'location_id' => 'required|exists:locations,id', | |
'cur_location_id' => 'required|exists:locations,id', | |
'chasis' => 'nullable|unique:vehicles,chasis', | |
'plate' => 'nullable|unique:vehicles,plate' | |
]; | |
} | |
case 'PUT': | |
case 'PATCH': | |
{ | |
$reglas = [ | |
'tag' => 'required|unique:vehicles,tag,' . $this->vehicle, | |
'vehicle_status_id' => 'required|exists:vehicle_status,id', | |
'item_id' => 'required|exists:items,id', | |
'location_id' => 'required|exists:locations,id', | |
'cur_location_id' => 'required|exists:locations,id', | |
'chasis' => 'nullable|unique:vehicles,chasis,' . $this->vehicle, | |
'plate' => 'nullable|unique:vehicles,plate,' . $this->vehicle | |
]; | |
} | |
default:break; | |
} | |
if (count($reglas)>0 && $this->user()->roll_id==1) { | |
$reglas['company_id'] = 'required|exists:companies,id'; | |
} | |
return $reglas; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment