1 year ago

database design / one to many

Posted 1 year ago by t0berius

I've got two questions.

1.) My product model will contain a relation to product images.

public function productImages()
    return $this->hasMany('App\ProductImage');

Every product will have one "main" product image. Is it better to save the "id" of the main image as an attribute of the productModel or should I save the "is_main_image" attribute on the productImage model? I think the first way is the better one.

My migration on the image model at the moment looks like this:

    Schema::create('product_images', function (Blueprint $table) {
        $table->string('name', 32);

2.) Is there a way to prevent laravel from loading the productImages() relation on each time I retrieve a product model (background: not on every model call of product I will need the images, or is this unimportant regarding "performance")?

Please sign in or create an account to participate in this conversation.