1

Hello guys i am new in PhP and Laravel. I want to use a join query in laravel and have written the code below, but it gives me an error.

$data = DB::table('employees')
->join('contact_details', 'employees.id', '=','contact_details.employee_id')
          ->join('education', 'employees.id', '=', 'education.employee_id')
          ->join('addresses', 'employees.id', '=', 'addresses.employee_id')
          ->select('employees.first_name','employees.id','employees.gender','education.id','education.school','education.subject','addresses.village')
          ->where('employees.id', '=',$id)


          ->get();
  return "$data";

and the error is

 ErrorException in ProfileController.php line 66:
 Array to string conversion

 in ProfileController.php line 66
 at HandleExceptions->handleError('8', 'Array to string conversion',        'E:\xampp\htdocs\djiafg\app\Http\Controllers\ProfileController.php', '66',      array('id' => '72', 'data' => array())) in ProfileController.php line 66
      at ProfileController->view('72')
     at call_user_func_array(array(object(ProfileController), 'view'), array('id'    => '72')) in Controller.php line 80
   at Controller->callAction('view', array('id' => '72')) in       ControllerDispatcher.php line 146
  at ControllerDispatcher->call(object(ProfileController), object(Route), 'view')   in ControllerDispatcher.php line 94
  at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request))
  at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
 at Pipeline->Illuminate\Routing\{closure}(object(Request))
 at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
3
  • 1
    Post your error too in question and remove that extra paranthesis from ($id) i.e. ->where('employees.id', '=',$id) Commented Apr 20, 2016 at 5:50
  • @Nazir Noori : It seems $id would be with parenthesis but your error report may well clarify the actual situation. Commented Apr 20, 2016 at 6:14
  • Don't post the error. That would be too easy. Commented Apr 20, 2016 at 6:59

2 Answers 2

3

Laravel has a special functions for joins, called models.

The Eloquent ORM included with Laravel provides a beautiful, simple ActiveRecord implementation for working with your database. Each database table has a corresponding "Model" which is used to interact with that table. Models allow you to query for data in your tables, as well as insert new records into the table.

A model can have one or many. This will return one row from the user table data.

Add this to your model which fetch data from your employees table.

public function user() {
  return $this->hasOne('app\User', 'id', 'user_id');
}

now you can run to dump the user id. (you need a model called "Employees" which is connected to your table "employees")

$employees = Employees::get();
dump($employees->user->id);

I hope this works!

See more at: https://laravel.com/docs/5.2/eloquent

Sign up to request clarification or add additional context in comments.

Comments

0

Just try to remove the space before select and make the where condition as follows

$data = DB::table('employees')
    ->join('contact_details', 'employees.id', '=','contact_details.employee_id')
    ->join('education', 'employees.id', '=', 'education.employee_id')
    ->join('addresses', 'employees.id', '=', 'addresses.employee_id')
    ->select('employees.first_name','employees.id','employees.gender','education.id','education.school','education.subject','addresses.village')
    ->where('employees.id',$id)
    ->get();

    return view ('profile',compact('data'));

1 Comment

Is that $id is array ? Can you try to print the $id

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.