0
votes

I have four tables.

  • User (user_id)
  • Orders (order_id & user_id)
  • Order_detail (post_id, order_id, product_id)
  • Product (comment_id)

How to create relationship between user table and product table? I have managed to create relationships between the other tables.

2

2 Answers

0
votes
class User extends Authenticatable
{
    public function orders() {
        return $this->hasMany('App\Order');
    }
}

class Product extends Model
{
    public function orderDetails() {
        return $this->hasMany('App\OrderDetail');
    }
}

class Order extends Model
{
    public function user() {
        return $this->belongsTo('App\User');
    }
    public function orders() {
        return $this->hasMany('App\Order');
    }
}

class OrderDetail extends Model
{
    public function product() {
        return $this->belongsTo('App\Product');
    }
    public function order() {
        return $this->belongsTo('App\Orders');
    }
}
0
votes

in User model:

use App\Models\Product; \\or your Product model path
class User extends Authenticatable{

public function products(){
    return $this->hasMany(Product::class);
}
}

and in Product model:

use App\Models\User; \\or your User model path

class Product extends Model{

public function user(){
    return $this->belongsTo(User::class);
}
}