I'm trying to do the INNER JOIN using Query Builder. But when I try to retrieve this all in my tables it says missing argument 2.
Missing argument 2 for Illuminate\Database\Query\Builder::join(), called in C:\Users\JohnFrancis\LaravelFrancis\app\Http\Controllers\DocumentController.php on line 63 and defined
I don't know if my ALIAS is wrong or not but I just based on my SQL queries. See below.
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
Database Diagram
Controller
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);
}
I wanted to retrieve but EXCEPT for the current user. As you can see here I added the where clause. ->where('sender_id', '!=', Auth::id()->get());
View
<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>
After reading a lot of blog and I find this lessons is useful.
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();