Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

148
Visualizações
Many to Many in Laravel

I can't quite wrap my head around this.

I have a product, that a user can make an offer on.

A user can make an offer on many products, and a product can have many offers at any given moment(think eBay but multiple people sending separate offers)

Do I need a many to many relationship for an offers migration for example, that references both the product and the user with associated fields in the database?

To me it should be

User->hasMany('App\Offers')
Product->hasMany('App\Offers')

On offers

Offer->belongsTo('App\Users')
Offer->belongsTo('App\Product')

Obviously this doesn't use many to many but because I have three models im interacting with, how is it best to model this?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

you should create product_offers pivot table .

Offer model :

class Offer extends Model
{

    public function products(){
        $this->belongsToMany('App\Product', product_offers);
    }


    public function users(){
        $this->belongsTo('App\User');
    }

Product Model:

class Product extends Model
{


    public function offers(){
        $this->belongsToMany('App\Offer', product_offers);
    }

}

User Model:

class User extends Model
{


    public function offers(){
        $this->hasMany('App\Offer');
    }

}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda