Francisunoxx Francisunoxx - 3 months ago 12
PHP Question

Column not found: 1054 Unknown column using join

I'm trying to retrieve the values based on the user's selected that I joined using

first
method. But when I tried to put the values in my form. It says


SQLSTATE[42S22]: Column not found: 1054 Unknown column 'sent_document_user.document.id' in 'on clause' (SQL: select
documents
.
title
,
categories
.
category_type
,
documents
.
content
,
documents
.
id
from
sent_document_user
inner join
documents
on
documents
.
id
=
sent_document_user
.
document
.
id
inner join
categories
on
categories
.
id
=
documents
.
category_id
where
documents
.
id
= 241 limit 1)``


I joined it properly I don't know why I can't retrieve the values that I selected. This is my code in SQL that works well.

SELECT D.title, C.category_type, D.content, D.id FROM sent_document_user SD
INNER JOIN documents D ON SD.document_id = D.id
INNER JOIN categories C ON D.category_id = C.id;


Controller

public function readSentDocuments($id)
{
$documentLists = DB::table('sent_document_user')->select('documents.title', 'categories.category_type', 'documents.content', 'documents.id')
->join('documents', 'documents.id', '=', 'sent_document_user.document.id')
->join('categories', 'categories.id', '=', 'documents.category_id')
->where('documents.id', '=', $id)->first();

return view ('document.readSent')->with('documentLists', $documentLists);
}


View

<div class = "form-group">

<label for = "title" class = "control-label">Title:</label>
<input type = "text" name = "title" class = "form-control" value = "{{ $documentLists->title }}">

</div>

<div class = "form-group">

<label for = "category" class = "control-label">Category:</label>
<input type = "text" name = "category" class = "form-control" value = "{{ $documentLists->category_type }}">

</div>

<div class = "form-group">

<textarea id = "content">{{ $documentLists->content }}</textarea>

</div>

Answer

It seems you have an incorrect column name

 ->join('documents', 'documents.id', '=', 'sent_document_user.document.id')

Try sent_document_user.document_id instead of sent_document_user.document.id

Comments