Files
freeu-project/app/Http/Requests/UpdateBlogRequest.php
Ritikesh yadav c661166e1d first commit
2024-03-28 14:52:40 +05:30

82 lines
2.2 KiB
PHP

<?php
namespace App\Http\Requests;
use App\Models\Admin\Blog;
use Illuminate\Foundation\Http\FormRequest;
use Cviebrock\EloquentSluggable\Services\SlugService;
class UpdateBlogRequest 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('id', '!=', $this->blog_id)->where('tag_id', request()->input('tag_id'))->count() > 0;
if ($question_exists) {
$fail('The title must be must be unique for this tag.');
}
},
],
"blog_description" => 'required',
// "tag_id" => 'required',
];
}
public function messages()
{
return [
'required' => 'The :attribute field is required.',
'unique' => 'The :attribute field is unique.',
];
}
public function validated()
{
$blog = Blog::find($this->blog_id);
$image = basename($blog->blog_image);
if ($this->has('blog_image')) {
if (\File::exists(public_path('/uploads/blog/images/' . $blog->blog_image . ''))) {
\File::delete(public_path('/uploads/blog/images/' . $blog->blog_image . ''));
}
$image = time() . '.' . $this->blog_image->extension();
$this->blog_image->move(public_path('/uploads/blog/images'), $image);
}
return array_merge(parent::validated(), [
'slug' => SlugService::createSlug(Blog::class, 'slug', $this->blog_title),
'blog_image' => $image
]);
}
}