Skip to content

Instantly share code, notes, and snippets.

@evercode1
Created February 12, 2017 00:53
Show Gist options
  • Save evercode1/63debac91999dc7d57b7e04c279eab3a to your computer and use it in GitHub Desktop.
Save evercode1/63debac91999dc7d57b7e04c279eab3a to your computer and use it in GitHub Desktop.
chapter 15 LessonCreateRequest.php
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Request;
use App\Category;
use App\Subcategory;
use Illuminate\Validation\Factory as ValidationFactory;
class LessonCreateRequest extends FormRequest
{
public function __construct(ValidationFactory $validationFactory)
{
$validationFactory->extend(
'belongsToCategory',
function ($attribute, $value, $parameters) {
$category_id = request()->input('category_id');
$verified = Subcategory::where('id', $value)
->where('category_id', $category_id)
->exists();
return 'belongsToCategory' == $verified;
},
'Sorry, subcategory does not belong to category!'
);
}
/**
* 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()
{
$count = Category::count();
return [
'name' => 'required|unique:lessons|string|max:30',
'category_id' => "required|numeric|digits_between:1,$count",
'subcategory_id' => 'required|belongsToCategory'
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment