Files
freeu-project/app/Http/Requests/StoreBlogRequest.php

76 lines
1.9 KiB
PHP
Raw Normal View History

2024-03-28 14:52:40 +05:30
<?php
namespace App\Http\Requests;
use App\Models\Admin\Blog;
use Illuminate\Foundation\Http\FormRequest;
use Cviebrock\EloquentSluggable\Services\SlugService;
class StoreBlogRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get custom attributes for validator errors.
*
* @return array
*/
public function attributes()
{
return [
'tag_id' => 'tag',
];
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
"blog_title" => [
'required',
function ($attribute, $value, $fail) {
// check question is unique in that table
$question_exists = Blog::where('blog_title', $value)->where('tag_id', request()->input('tag_id'))->count() > 0;
if ($question_exists) {
$fail('The title must be unique for this tag.');
}
}
],
"blog_description" => 'required',
"blog_image" => 'required',
// "tag_id" => 'required',
];
}
public function messages()
{
return [
'required' => 'The :attribute field is required.',
'unique' => 'The :attribute field is unique.',
];
}
public function validated()
{
$blogStorageName = time() . '.' . $this->blog_image->extension();
$this->blog_image->move(public_path('/uploads/blog/images'), $blogStorageName);
return array_merge(parent::validated(), [
'slug' => SlugService::createSlug(Blog::class, 'slug', $this->blog_title),
'blog_image' => $blogStorageName
]);
}
}