2016-04-20 12 views
1

こんにちは、こんにちは。私はで新しいです.PPLaravellaravelで結合クエリを使用したいと思っていますが、以下のコードを書いていますが、エラーが表示されます。Laravel Join query

$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"; 

とエラーが

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 
+1

の質問にと'($ ID) 'すなわち'から、余分なparanthesisを削除:

はでより多くを参照してください。 –

+0

@ Nazir Noori:$ idはカッコで囲まれているようですが、エラー報告が実際の状況をはっきりと示すかもしれません。 – naf4me

+0

エラーを送信しないでください。それは簡単すぎるでしょう。 – Strawberry

答えて

0

$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')); 
+0

ハセナに感謝しましたが、うまくいきませんでした –

+0

その$ idは配列ですか?あなたは$ idを印刷しようとしますか? –

3

Laravelはモデルと呼ばれ、参加するための特別な機能を持っているし、次のようにただ選択の前にスペースを削除し、WHERE条件を作ってみるです。

Laravelに含まれているEloquent ORMは、データベースを操作するための美しくシンプルなActiveRecord実装を提供します。各データベーステーブルには、対応する「モデル」があり、それはそのテーブルと対話するために使用されます。モデルを使用すると、テーブル内のデータをクエリしたり、新しいレコードをテーブルに挿入することができます。

モデルは1つまたは複数のモデルを持つことができます。これにより、ユーザーテーブルのデータから1行が返されます。

従業員テーブルからデータを取得するモデルにこれを追加します。

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

これで、ユーザーIDをダンプすることができます。 (あなたは "従業員"というテーブルがあなたのテーブルの "従業員"に接続されている必要があります)

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

私はこれがうまくいきたいと思います! `>( 'employees.id'、 '='、$番号) - あまりにもあなたのエラーhttps://laravel.com/docs/5.2/eloquent