BelongsTo怎么实现增加一个字段的名称筛选
表结构:
| id | type | type_id | parent_id | name |
|---|---|---|---|---|
| 1 | type1 | 1 | 0 | type1-first |
| 2 | type1 | 2 | 1 | type1-second |
| 3 | type2 | 1 | 0 | type2-first |
| 4 | type2 | 2 | 1 | type2-second |
解释:
本表关联, type作为类型区分,type_id是对应不同type的主id,parent_id对应的是对应自己type的type_id。
直接belongsto ,id=4的数据将关联到type_id=1,但是第一个type_id=1的数据将实际匹配到id=1的数据,但是id=1的这行是属于type=type2的,结果错误。
问题:
也就是说怎么能够return ->belongsTo(static::class, 'parent_id', 'type_id')->这怎么加上个xxx('type','=','this.type') ?
关于 LearnKu
推荐文章: