I need to list 3 tables in the Standard, somebody help!!! I tried it anyway!

Asked

Viewed 22 times

-1

I have 3 tables

Sales

ID | TITULO

Brokers

ID | NOME

Relas - "Make the relationship between the tables"

ID | VENDAS_ID | CORRETORES_ID | TIPO

I need the table relas make the relationship between the tables

I tried my model like this :

  public function vendas()
    {
    return $this->belongsToMany(Vendas::class);
    }

    public function corretores()
    {
        return $this->belongsToMany(Corretores::class);
    }

My model sales like this:

  public function relas()
    {
        return $this->hasMany(Relas::class);
    }

My model brokers like this:

 public function relas()
    {
        return $this->hasMany(Relas::class);
    }

And my Migration RELAS like this:

    $table->id();
    $table->unsignedBigInteger("vendas_id");
    $table->unsignedBigInteger("corretores_id");
    $table->string("tipo");
    $table->timestamps();

    $table->foreign("vendas_id")->references("id")->on("vendas")->cascadeOnDelete();
    $table->foreign("corretores_id")->references("id")->on("corretores")->cascadeOnDelete();

Only I’m not able to make the relationship, because a sale has several brokers and various pickups, because of that I needed to make a separate relationship table, someone helps me?

1 answer

0

If I understand correctly I think what you need is the relationship Hasmanythrough.

In sales would have:

public function corretores()
{
   return $this->hasManyThrough(Corretores::class, Relas::class);
}

In the brokers would be:

public function vendas()
{
    return $this->hasManyThrough(Vendas::class, Relas::class);
}

So you can get the brokers of a sale and vice versa.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.