Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Kortez12's avatar

4 database tables and FormRequests

Hello all,

I have a problem and need an opinion about it.

I have four database tables: vehicles, trucks, trailers, and cars. The vehicles table has columns id, make, and model, while the other tables have columns vehicle_id, emission_type, and fuel_tank_size. When a vehicle record is created, a corresponding record in one of the vehicle type tables is also created.

I am unsure of the best approach for FormRequests in this situation. Should I create three separate FormRequests for trucks, trailers, and cars, and copy the same validation rules for the vehicles table into each of those requests? Or should I create a VehicleFormRequest and then merge the vehicles table validation rules into the TrailerFormRequest, TruckFormRequest, etc.?

My question is essentially this: Is it a good idea to duplicate the validation rules in three separate FormRequests, or is it better to create the validation rules once and merge them into the specific FormRequests? Also, what if I have another table vehicle_assignments which also cannot exist without vehicle record. What approach would be best to implement another FormRequest into this scenario? Your advice would be greatly appreciated. Thank you in advance.

0 likes
1 reply
LaryAI's avatar
Level 58

To address your problem, it's generally a good idea to avoid duplicating validation rules across multiple FormRequests. Instead, you can create a base VehicleFormRequest that contains the common validation rules for the vehicles table, and then extend this base request in your specific TruckFormRequest, TrailerFormRequest, and CarFormRequest. This way, you can keep your code DRY (Don't Repeat Yourself) and maintainable.

Here's how you can achieve this:

  1. Create a base VehicleFormRequest: This request will contain the common validation rules for the vehicles table.

    namespace App\Http\Requests;
    
    use Illuminate\Foundation\Http\FormRequest;
    
    class VehicleFormRequest extends FormRequest
    {
        public function authorize()
        {
            return true;
        }
    
        public function rules()
        {
            return [
                'make' => 'required|string|max:255',
                'model' => 'required|string|max:255',
            ];
        }
    }
    
  2. Create specific FormRequests for trucks, trailers, and cars: These requests will extend the VehicleFormRequest and add their own specific validation rules.

    namespace App\Http\Requests;
    
    class TruckFormRequest extends VehicleFormRequest
    {
        public function rules()
        {
            $rules = parent::rules();
    
            $rules = array_merge($rules, [
                'emission_type' => 'required|string|max:255',
                'fuel_tank_size' => 'required|integer|min:0',
            ]);
    
            return $rules;
        }
    }
    
    namespace App\Http\Requests;
    
    class TrailerFormRequest extends VehicleFormRequest
    {
        public function rules()
        {
            $rules = parent::rules();
    
            $rules = array_merge($rules, [
                'emission_type' => 'required|string|max:255',
                'fuel_tank_size' => 'required|integer|min:0',
            ]);
    
            return $rules;
        }
    }
    
    namespace App\Http\Requests;
    
    class CarFormRequest extends VehicleFormRequest
    {
        public function rules()
        {
            $rules = parent::rules();
    
            $rules = array_merge($rules, [
                'emission_type' => 'required|string|max:255',
                'fuel_tank_size' => 'required|integer|min:0',
            ]);
    
            return $rules;
        }
    }
    

By following this approach, you ensure that the common validation rules for the vehicles table are defined in one place, and any specific rules for trucks, trailers, and cars are added in their respective FormRequests. This makes your code more maintainable and easier to manage.

Please or to participate in this conversation.