Answer the question
In order to leave comments, you need to log in
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
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');
}
}
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();
}
}
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');
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question