first commit
This commit is contained in:
81
app/Http/Requests/UpdateBlogRequest.php
Normal file
81
app/Http/Requests/UpdateBlogRequest.php
Normal file
@@ -0,0 +1,81 @@
|
||||
<?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
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user