Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

147
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!