Thinkphp5.1模型 with操作关联表字段

1.withjoin是一种方法  

static function getVisitResult()
    {
        $result = self::withjoin('userItems','LEFT')
            ->where('userItems.id','=',1)
            ->order('id desc')
            ->select()->toArray();

        return $result;
    }

2.with 闭包

static function getVisitResult()
    {
        $result = self::with(['userItems' => function($query){
            $query->where('id',1);
        }])
            ->with('companyItems')
            ->order('id desc')
            ->select()->toArray();

        return $result;
    }

 

你可能感兴趣的:(Thinkphp5.1模型 with操作关联表字段)