V
V
Vlad Avtomat2017-08-08 21:47:53
Laravel
Vlad Avtomat, 2017-08-08 21:47:53

How to properly link a table in laravel?

Let's start over:
I write in migration

Schema::table('partner_products', function (Blueprint $table) {
            $table->integer('partner_products_statuses_id')->default(4);
            $table->foreign('partner_products_statuses_id')
                ->references('id')->on('partner_products_statuses');
        });

In the PartnerProduct model
public function partnerProductsStatus()
    {
        return $this->hasOne(PartnerProductsStatus::class, 'id' , 'partner_products_statuses_id');
    }

In the PartnerProductsStatus model
public function partnerProduct()
    {
        return $this->belongsTo(PartnerProduct::class, 'partner_products_statuses_id', 'id');
    }

Why when I look through PhpStorm the connection is not visible?
And also the table icon is not displayed when adding this column.
There is no entry for this column in the DDL.
For example, something like this:
partner_id integer not null
    constraint partner_products_partner_id_foreign
      references partners
        on update cascade on delete cascade,

But from the last migration, for example, in DDL there is a record about the last migration
. Although I deleted this column through migration.
product_status varchar(255)
Help me figure out what I'm doing wrong.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex Wells, 2017-08-08
@Alex_Wells

Why do you need a pivot table? You have a one to many relationship, not many to many.
Remove the migration completely, replace the methods with

public function partnerProductsStatus()
{
    return $this->hasOne(PartnerProductsStatus::class);
}

public function partnerProduct()
{
    return $this->belongsTo(PartnerProduct::class);
}

And then as usual. Laravel will take care of everything.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question