Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

229
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda