Skip to content

Instantly share code, notes, and snippets.

@ckentq
Last active December 24, 2021 02:10
Show Gist options
  • Save ckentq/1923aea64473020ae3a36506d87a80fe to your computer and use it in GitHub Desktop.
Save ckentq/1923aea64473020ae3a36506d87a80fe to your computer and use it in GitHub Desktop.
<?php
....略
/**
* Store a newly created resource in storage.
*
* @param UserRequest $request
*
* @return Application|ResponseFactory|JsonResponse|Response
*/
public function store(CustomRequest $request)
{
CustomModel::create( $request->validated() );
}
<?php
namespace App\Api\Requests;
use Illuminate\Foundation\Http\FormRequest;
class CustomRequest extends FormRequest
{
public function passedValidation()
{
$this->merge(
['date' => \Carbon\Carbon::parse($this['date'])->format('Y-m-d')]
);
}
/**
* 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()
{
switch ($this->method()) {
case 'POST':
return [
'member_id' => 'required|uuid|exists:members,id',
'date' => 'required|date_format:Y/m/d',
'meal_time' => 'required|date_format:H:i',
'meal_type' => 'required|string|in:breakfast,lunch,dinner,other',
'restaurant' => 'required|array',
'restaurant.id' => 'required|integer',
'restaurant.name' => 'sometimes|string',
'dishes' => 'required|array',
'dishes.*.id' => 'required|integer',
'dishes.*.name' => 'sometimes|string|nullable',
'dishes.*.image' => 'sometimes|string|nullable',
'dishes.*.unit_quantities' => 'required|numeric',
'dishes.*.rating' => 'required|numeric',
'dishes.*.remark' => 'sometimes|string|nullable',
];
case 'PUT':
return [
'member_id' => 'required|uuid|exists:members,id', //驗證資料庫內欄位members.id
'id' => 'required|integer|exists:daily_meal_dish,id',
'meal_time' => 'required|string|',
'meal_type' => 'required|string|in:breakfast,lunch,dinner,other',
'dish' => 'required|array',
'dish.name' => 'sometimes|string|nullable',
'dish.image' => 'sometimes|string|nullable',
'dish.unit_quantities' => 'required|numeric',
'dish.rating' => 'required|numeric',
'dish.remark' => 'sometimes|string|nullable',
];
default:
return [];
}
}
public function attributes()
{
return [
'member_id' => 'Membmer ID',
'id' => 'Meal Dish Record ID',
'date' => 'Date',
'meal_time' => 'Meal Time',
'meal_type' => 'Meal Type',
'restaurant' => 'Restaurant Object',
'restaurant.id' => 'Restaurant ID',
'restaurant.name' => 'Restaurant Name',
'dishes' => 'Dish Array', //陣列
'dishes.*.id' => 'Dish ID', //陣列子內容
'dishes.*.name' => 'Dish Name',
'dishes.*.image' => 'Dish Image Url',
'dishes.*.unit_quantities' => 'Dish Unit Quantities',
'dishes.*.rating' => 'Dish Rating',
'dish' => 'Dish Object',
'dish.id' => 'Dish ID',
'dish.name' => 'Dish Name',
'dish.image' => 'Dish Image Url',
'dish.unit_quantities' => 'Dish Unit Quantities',
'dish.rating' => 'Dish Rating',
'dish.remark' => 'Remark',
];
}
public function messages()
{
return [
'required' => ' :attribute 未輸入',
'exists' => ':attribute 不存在',
'string' => ':attribute 必須是字串',
'integer' => ':attribute 必須是整數',
'date' => ':attribute 必須是日期',
'numeric' => ':attribute 必須是數字',
'array' => ':attribute 必須是陣列',
'boolean' => ':attribute 必須是布林',
'min' => ':attribute 不能小於:min',
'max' => ':attribute 不能大於:max',
'between' => ':attribute 的值 :input 不在 :min - :max 之間',
];
}
/**
* Prepare the data for validation.
*
* @return void
*/
protected function prepareForValidation()
{
<!-- 預先組合加入驗證 -->
$this->merge([
'slug' => Str::slug($this->slug),
]);
}
/**
* Configure the validator instance.
*
* @param \Illuminate\Validation\Validator $validator
* @return void
*/
public function withValidator($validator)
{
$validator->after(function ($validator) {
if ($this->somethingElseIsInvalid()) {
$validator->errors()->add('field', 'Something is wrong with this field!');
}
<!-- 驗證過後才組合資訊部分 -->
$this->merge([
'address' => $this->city.$this->district.$this->street,
]);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment