Cart:ID | user Cart_goods:cart_ID | good_ID | good_typeDetails:ID | nameAppliances: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,可以是Detail或Appliance,相对于cart的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(); }}
详细类:
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'); }}
我需要把所有的电器和细节放在购物车里.如何使用ORM?
使用 nested eager loading.假设关系方法名称是cartGoods,详细信息和设备,详细信息是购物车的详细信息,并且您已经定义了关系权限:Card::where('ID',$ID) ->with('cartGoods.appliances','details') ->first();总结
以上是内存溢出为你收集整理的php – Laravel中的模型关系5.3全部内容,希望文章能够帮你解决php – Laravel中的模型关系5.3所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)