2016-07-24 71 views
0

我正在尝试使用查询生成器来执行INNER JOIN。但是,当我试图找回这一切都在我的表,它说缺少参数2.如何选择使用Inner Join?

缺少参数2照亮\数据库\查询\生成器:: join()方法,称为C:\用户\ JohnFrancis \ LaravelFrancis \ app \ Http \ Controllers \ DocumentController.php on line 63 and defined

我不知道我的ALIAS是否错误,但我只是基于我的SQL查询。见下文。

SQL

SELECT D.title, C.category_type, U.username, DU.dateReceived FROM document_user DU 
#1st JOIN 
INNER JOIN users U ON DU.sender_id = U.id 
#2nd JOIN 
INNER JOIN documents D ON DU.document_id = D.id 
#3rd JOIN 
INNER JOIN categories C ON C.id = D.category_id; 

结果

Result

数据库图表

SC

控制器

public function showDocuments() 
{ 

    $documentsList = DB::table('document_user') 
     ->select('D.title', 'C.category_type', 'U.username', 'DU.dateReceived') 
     ->join('users AS U')->on('DU.sender_id', '=', 'U.id') 
     ->join('documents AS D')->on('DU.document_id', '=', 'D.id') 
     ->join('categories AS C')->on('C.id', '=', 'D.category_id') 
     ->where('sender_id', '!=', Auth::id()->get()); 

    return view ('document.show')->with('documentsList', $documentsList); 
} 

我想取回但EXCEPT当前用户。正如你在这里看到的,我添加了,其中条款。 ->where('sender_id', '!=', Auth::id()->get());

查看

<table class = "table"> 

     <thead> 
      <tr> 
       <th>Title</th> 
       <th>Category</th> 
       <th>Sender</th> 
       <th>Date Received</th> 
       <th>Action</th> 
      </tr>    
     </thead> 

     <tbody> 
     @foreach ($documentsList as $list) 
      <tr class = "info"> 
      <td>{{ $list->title }}</td> 
      <td>{{ $list->category_type }}</td> 
      <td>{{ $list->username }}</td> 
      <td>{{ $list->dateReceived }}</td> 
      <td><a href = "#"><button type = "submit" class = "btn btn-info">Read</button></a></td> 
      </tr> 
     @endforeach 
     </tbody> 

</table> 
+0

$此加入(字符串$表,字符串$之一,字符串$运算符= NULL,串2 $ = NULL,字符串$类型= '内',布尔$其中= FALSE) – ClearBoth

+0

@ClearBoth什么? – Francisunoxx

回答

0

阅读大量的博客后,我觉得这个教训是非常有用的。

http://jpcamara.com/selecting-carefully-laravel-joins/

$documentsList = DB::table('document_user')->select('documents.title', 'categories.category_type', 'users.username', 'document_user.dateReceived') 
     ->join('users', 'users.id', '=', 'document_user.sender_id') 
     ->join('documents', 'documents.id', '=', 'document_user.document_id') 
     ->join('categories', 'categories.id', '=', 'documents.category_id') 
     ->where('sender_id', '!=', Auth::id())->get();