D
D
Detryer2017-02-07 12:01:59
Laravel
Detryer, 2017-02-07 12:01:59

How to implement relationships in Laravel 5.3?

I have the following MySQL tables for a shopping cart

Cart:
id | user

Cart_goods:
cart_id | good_id | good_type

Details:
id | name

Appliances:
id | name

The Cart class that stores the products:
class Cart extends Model
{
 protected $table = 'cart';
 protected $fillable = ['user', 'sum', 'profit', 'discount'];
 protected $guarded = ['user'];

 public function goods()
 {
    return $this->hasMany('App\Models\Good');
 }
}

Good class, which can be Detail or Appliance and refers to the cart by cart_id:
class Good extends Model
{
 protected $table = 'cart_goods';
 protected $types = [
    'Detail' => 'App\Models\Detail',
    'Appliance' => 'App\Models\Appliance'
 ];
 public $primaryKey = 'cart_id';

 public function good()
 {
    return $this->morphTo();
 }
}

Detail class:
class Detail extends Model {
 use SoftDeletes;

 protected $morphClass = 'Detail';
 protected $table = 'details';
 protected $fillable = ['article', 'name', 'photo', 'price_procurement', 'price_retail', 'serial', 'location_id', 'type_id', 'appliance_id', 'comment', 'for'];
 protected $dates = ['deleted_at'];
 protected $guarded = [];

 public function goods()
 {
    return $this->morphMany('App\Models\Good', 'good');
 }
}

And I need to get all the Detail and Appliance that are in a particular cart. How can I implement this with an ORM?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question