insert laravel relation data from two table to one in two column only both column are UNSIGNED

To insert Laravel relation data from two tables to one in two columns only, both columns are UNSIGNED, you can follow these steps:

  1. Define the relationships in the respective models:

    • ProductAttribute model:
    namespace App\Models;

    use Illuminate\Database\Eloquent\Model;

    class ProductAttribute extends Model
    {
        /**
         * Get the attribute value for the product attribute.
         */
        public function attributeValue()
        {
            return $this->belongsTo('App\Models\AttributeValue');
        }

        /**
         * Get the product for the product attribute.
         */
        public function product()
        {
            return $this->belongsTo('App\Models\Product');
        }
    }

AttributeValue model:

    namespace App\Models;

    use Illuminate\Database\Eloquent\Model;

    class AttributeValue extends Model
    {
        /**
         * Get the product attributes for the attribute value.
         */
        public function productAttributes()
        {
            return $this->hasMany('App\Models\ProductAttribute');
        }
    }

Product model:

    namespace App\Models;

    use Illuminate\Database\Eloquent\Model;

    class Product extends Model
    {
        /**
         * Get the product attributes for the product.
         */
        public function productAttributes()
        {
            return $this->hasMany('App\Models\ProductAttribute');
        }
    }

To insert data into the product_attributes table, you can use the create method on the ProductAttribute model and pass in an associative array of values for the related tables. Assuming that the attribute_value_id and product_id columns in the product_attributes table are UNSIGNED, you can use the unsignedBigInteger method to create the migration for these columns in the product_attributes table:

    Schema::create('product_attributes', function (Blueprint $table) {
        $table->id();
        $table->unsignedBigInteger('attribute_value_id');
        $table->unsignedBigInteger('product_id');
        $table->foreign('attribute_value_id')->references('id')->on('attribute_values')->onDelete('cascade');
        $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
        $table->timestamps();
    });

Then you can insert data into the product_attributes table using the create method as follows:

    $product = Product::find($productId);
    $attributeValue = AttributeValue::find($attributeValueId);

    $productAttribute = $product->productAttributes()->create([
        'attribute_value_id' => $attributeValue->id,
    ]);

    // OR

    $productAttribute = ProductAttribute::create([
        'attribute_value_id' => $attributeValue->id,
        'product_id' => $product->id,
    ]);

Note that you can use either method to insert data into the product_attributes table. The first method uses the create method on the relationship between Product and ProductAttribute, while the second method uses the create method directly on the ProductAttribute model.

  1. Also note that the create method automatically sets the product_id column to the ID of the $product variable, since it is being called on the productAttributes relationship.

Did you find this article valuable?

Support Mandeep Singh Blog by becoming a sponsor. Any amount is appreciated!