Laravel 4.2 for many relationships, use something else and then the default id

I have 2 models, and both of them do not use the identifier from the table, and the field is internal_id. So I set up my summary diagram, but I was stuck on connecting them. Im getting the error:

General error: 1215 Cannot add foreign key constraint (SQL: alter table `seoshop_category_product` add constraint seoshop_category_product_category_id_foreign foreign key   
  (`category_id`) references `seoshop_categories` (`internal_id`) on delete cascade)                                                                                                            

Transfer Code:

Schema::create('seoshop_category_product', function(Blueprint $table)
        {
            $table->increments('id');
            $table->integer('category_id')->unsigned()->index();
            $table->foreign('category_id')->references('internal_id')->on('seoshop_categories')->onDelete('cascade');
            $table->integer('product_id')->unsigned()->index();
            $table->foreign('product_id')->references('internal_id')->on('seoshop_products')->onDelete('cascade');
            $table->timestamps();
        });

Both fields like seoshop_products.internal_id as seoshop_categories.internal_id exist, column types are like int (11).

Can someone tell me what is going wrong?

Migrations for seoshop_categories and seoshop_products tables

//seoshop_products
public function up()
    {
        Schema::create('seoshop_products', function(Blueprint $table)
        {
            $table->increments('id');
            $table->integer('shop_id');
            $table->integer('internal_id')->signed()->index();
            $table->integer('internal_variant_id');
            $table->string('visible');
            $table->string('tags');
            $table->timestamps();
        });
    }


//Table seoshop_categories
public function up()
    {
        Schema::create('seoshop_categories', function(Blueprint $table)
        {
            $table->increments('id');
            $table->integer('internal_id')->signed()->index();
            $table->datetime('seoshop_created_at');
            $table->datetime('seoshop_updated_at');
            $table->text('full_description');
            $table->timestamps();
        });
    }

Ok, now I created my table and its work as needed. I need to get my product with categories (many-many-many). Therefore i use

SEOshopProduct::find(1)->with('categories')->get();

After dd (), the categories are empty, and I looked through my query, as it is called:

[8] array(3) {
["query"] "select `seoshop_categories`.*, `seoshop_category_product`.`product_id` as `pivot_product_id`, `seoshop_category_product`.`category_id` as `pivot_category_id` from `seoshop_categories` inner join `seoshop_category_product` on `seoshop_categories`.`id` = `seoshop_category_product`.`category_id` where `seoshop_category_product`.`product_id` in (?)"
["bindings"] array(1) {
[0] 8
}
["time"] 0.37
}

_ 10.000, .

:

:

public function categories(){
        return $this->belongsToMany('SEOshopCategory', 'seoshop_category_product', 'product_id', 'category_id');
    }

:

public function products(){
        return $this->belongsToMany('SEOshopCategory', 'seoshop_category_product', 'category_id', 'product_id');
    }
+4
2

, . , , seoshop_category_product.category_id UNSIGNED INT, seoshop_categories.internal_id SIGNED INT. .

, internal_id , , , , .

+2

Laravel, , ...

class Product extends Eloquent
{
    public function categories() {
        return $this->hasMany('Category', 'internal_id', 'id');
    }
}

class Category extends Eloquent
{
    public function products() {
        return $this->hasMany('Product', 'internal_id', 'id');
    }
}
-1

Source: https://habr.com/ru/post/1617056/


All Articles