dimanche 26 juin 2016

User, Cart and Products Eloquent Relationship

I would like to ask about the User, Cart and Product eloquent relationships. The User hasOne Cart and the Cart can have many Product.

class User {

    public function cart()
    {
        return $this->hasOne(App\Cart::class);
    }
}



class Cart {

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

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



class Product {

    //
}

I have database table structured like:

users

- id
- email
- username
- password

carts

- id
- user_id
- product_id
- quantity

products

- id
- name
- price

Is this correct?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/28VTPXA
via IFTTT

Aucun commentaire:

Enregistrer un commentaire