1

I am using laravel 5.4. When trying to run

select * from `users` inner join `addprojects` on `users`.`emp_id` = `addprojects`.`emp_id` where `emp_id` = $emp_id)"

It produces:

"SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'emp_id' in where clause is ambiguous (SQL: select * from users inner join addprojects on users.emp_id = addprojects.emp_id where emp_id = $emp_id)"

$emp_id= Auth::user()->emp_id;
$projects_for_emp = DB::table('users')->join('addprojects', 'users.emp_id', '=', 'addprojects.emp_id')->where('emp_id', '$emp_id')->get();

3 Answers 3

3

emp_id in both tables , so can use users.emp_id or addprojects.emp_id

->where('users.emp_id', '=', $emp_id)
Sign up to request clarification or add additional context in comments.

Comments

2

You have specified column name which exists in both tables, so use users.emp_id or addprojects.emp_id doesn't matter which one

Example:

$emp_id= Auth::user()->emp_id;
$projects_for_emp = DB::table('users')
   ->join('addprojects', 'users.emp_id', '=', 'addprojects.emp_id')
   ->where('users.emp_id', '$emp_id')
   ->get();

Comments

1

Because both users and addprojects tables have same named field "emp_id". You can change your code like following.

$emp_id= Auth::user()->emp_id;
$projects_for_emp = DB::table('users')
    ->join('addprojects', 'users.emp_id', '=', 'addprojects.emp_id')
    ->where('users.emp_id', $emp_id)
    ->get();

1 Comment

Thanks it works. but i think where condition does not work properly . If I return $projects_for_emp; It shows jeson word with blank page. But If I ->where('users.emp_id', '245') , It will Show value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.