0

I has UserRolePlan that belongsTo RoleInfo

class UserRolePlan extends Model
{        
    public function roleInfo()
    {
        return $this->belongsTo(RoleInfo::class,'role_info_id');
    }    
}
 

I get the desired result with this code

\DB::table('user_role_plans')
 ->join('role_infos', 'user_role_plans.role_info_id', '=', 'role_infos.role_info_id')            
 ->select('user_role_plans.user_role_plan_id', 'role_infos.is_active')
 ->get()

But with this code

UserRolePlan::with("roleInfo")
->select("user_role_plans.user_role_plan_id", "roleInfo.is_active")
->get()

I get error that Unknown column 'roleInfo.is_active'.

How can select column from relationship besides main table?

I want relationship column and base table together...

ashkufaraz
  • 5,179
  • 6
  • 51
  • 82
  • `->with()` doesn't perform any kind of `join()`, so you can't use `->select('table.joined.column')`. The correct syntax is `->with('roleInfo:role_info_id ,is_active')`, which would make `$userPlanRole->roleInfo->is_active` available. – Tim Lewis Feb 15 '23 at 18:07
  • The linked duplicate shows this logic in a couple different formats. – Tim Lewis Feb 15 '23 at 18:07

0 Answers0