Laravel Customize Query Builder

For my situation, I need to use complex query conditions for my cases, I want to aggregate my conditions in my entity, so I decided to extend the query builder. We know laravel providing the method to allow you customize Eloquent builder: newEloquentBuilder, but I want to more easy to use, almostly, I want to out of the box. So I’m following below to think, starting with my ideal. Model class User extend Model // you can set the contract....

December 4, 2021 · 2 min · Yish

Laravel Customize Failed Validation Request

In this post, I will show you how to customize the form request validates failed response correctly, it’s really simple but useful. <?php namespace App\Http\Requests\Concerns; use App\Enums\Response; use Illuminate\Contracts\Validation\Validator; use Illuminate\Http\Exceptions\HttpResponseException; use Illuminate\Http\JsonResponse; use Illuminate\Validation\ValidationException; trait HandleFailedValidation { protected function failedValidation(Validator $validator): HttpResponseException { if ($this->expectsJson()) { // if you're json $errors = (new ValidationException($validator))->errors(); // take all errors as array. throw new HttpResponseException( new JsonResponse([ 'status' => Response::FAILED_VALIDATION, // customize status code....

December 3, 2021 · 1 min · Yish