Skip to content

Instantly share code, notes, and snippets.

@kikoseijo
Last active May 4, 2017 01:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kikoseijo/2af113852b77f18e71d6f4ec090f4580 to your computer and use it in GitHub Desktop.
Save kikoseijo/2af113852b77f18e71d6f4ec090f4580 to your computer and use it in GitHub Desktop.
Laravel unique rules on FormRequest
<?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