[英]How to Select using Inner Join?
我正在嘗試使用查詢生成器進行INNER JOIN 。 但是,當我嘗試在表中檢索所有內容時,它說缺少參數2。
第63行的C:\\ Users \\ JohnFrancis \\ LaravelFrancis \\ app \\ Http \\ Controllers \\ DocumentController.php中調用的Illuminate \\ Database \\ Query \\ Builder :: join()缺少參數2
我不知道我的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;
結果
數據庫圖
控制者
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);
}
我想檢索但當前用戶除外 。 如您所見,我添加了where子句。 ->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>
看了很多博客后,我發現本課很有用。
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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.