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

Prido's avatar
Level 2

Model Relationship

Hi, i have a VehicleModel, FeatureCategoryModel & FeatureModel. These features can be 50+ across all categories. Each vehicle recorded has its features. Now i want to relate these features with the vehicle without bloating the db. I cnt have 50+ records for each vehicle that can make the db big fast.


class VehicleModel{

/**
* Fields list
 * [year] Date
 * [model_id] BigInt  
 * [user_id] BigInt // to see
 * [price] double
 * [condition] string ( new | used)
 * [location_id] bigInt
 * [description] Text
 */
}

class FeatureCategory{

/**
* Field list
 * name
 */
 
 public function features()
}


class FeatureModel{

/**
* Fields list
 * [feature_category_id] bigInt
 * [name] String
 * [surfix] String // ( one that adds to complete [name] eg. 1ltr )
 * [data_type] (double, string, boolean)
 * [icon] String 
 */
}
0 likes
3 replies
PovilasKorop's avatar

It should be a many-to-many, as @tray2 said.

Migration of feature_vehicle table:

$table->foreignId('feature_model_id')->constrained();
$table->foreignId('vehicle_model_id')->constrained();

Model:

class VehicleModel{
    public function features() {
        return $this->belongsToMany(FeatureModel::class. 'feature_vehicle');
    }
}
1 like
Prido's avatar
Level 2

thanks for the clarity your answers are all the best

Please or to participate in this conversation.