Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

noblemfd's avatar

How to resolve the problem: table doesn't exist in DB:Raw

In my Laravel-5.8, I have this query:

use DB;

    $published_goalss = DB::table('hr_employees e')
                ->join('hr_employees em','em.employee_code','=','e.line_manager_id')
                ->join('hr_departments d','e.department_id','=','d.id')
                ->join('hr_employees eh','eh.employee_code','=','d.dept_head')
                ->join('hr_employees eb','eb.employee_code','=','d.hr_business_partner_id')
                ->join('appraisal_goals ag','ag.company_id','=','e.company_id')
                 ->select(
                    'e.employee_code as staff_id',
                    DB::raw('CONCAT(e.first_name, " ", e.last_name) AS full_name'),
                    DB::raw('CONCAT(hr_employees.first_name, " ", hr_employees.last_name) AS full_name'),
                    DB::raw('IF(hr_employees.gender_code = 0, "Female", "Male") AS gender'),
                    'hr_employee.email as official_email',
                    DB::raw('CONCAT(eh.first_name, " ", eh.last_name) AS hod_name'),
                    DB::raw('CONCAT(eb.first_name, " ", eb.last_name) AS hrbp_name'),
                    DB::raw('(CASE WHEN ag.is_approved = 3 THEN "Approved" WHEN ag.is_approved = 2 THEN "Not Approved" WHEN ag.is_approved = 1 THEN "Awaiting Approval" ELSE "Draft" END) AS goal_status')
             )
                ->where('e.employee_code', $publishedgoals)
                ->where('e.company_id', $userCompany)
                ->where('e.hr_status', 0)                
            ->get();

When I ran it, I got this error:

Base table or view not found: 1146 Table 'myapp.hr_employees e' doesn't exist

But the table exists.

How do I resolve this?

Thanks

0 likes
2 replies
MichalOravec's avatar
Level 75

Use AS

DB::table('hr_employees AS e')

also

->join('hr_employees AS em','em.employee_code','=','e.line_manager_id')
tykus's avatar

The table alias is not working; you could be more explicit:

DB::table('hr_employees e')
	// -> etc

Please or to participate in this conversation.