Reputation: 33
I'm using the Laravel framework to create a prototype and need to output an individual article from a database based on it's ID. Seems straightforward but I keep getting the following error when I query the database:
QueryException in Connection.php line 647:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'articles.id' in 'where clause' (SQL: select * from `articles` where `articles`.`id` = 1 limit 1)
I'm trying to retrieve a specific article based on the database column article_id
via the articles table in my Article model. I am using a resource route thus the method name 'show' - so I can access via article/{$article_id}
in the URL.
Here is my controller:
public function show($article_id) {
$article = Article::find($article_id);
$article->toArray();
return view('articles.view', array('article' => $article));
}
There is no articles.id
. It should be articles_id
. Am I missing something obvious here?
Thanks in advance, any pointers much appreciated.
Upvotes: 0
Views: 1393
Reputation: 23011
The find command looks for whatever is set as the primary key, which by default is id
. You can change the primary key in your model:
class Article {
protected $table = 'articles';
protected $primaryKey = 'articles_id';
}
Upvotes: 3
Reputation: 163788
find()
searched by primary key, so change the query to:
$article = Article::where('article_id', $article_id)->first();
Upvotes: 2