Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

226
Views
SQL column not found although it exists

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.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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';

}
over 4 years ago · Santiago Trujillo Report

0

find() searched by primary key, so change the query to:

$article = Article::where('article_id', $article_id)->first();
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!