Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bayramgeldi's avatar

Hello I have problem on fetching data from database using CRUD

I want to fetch entries from database and show it. Entry ID will be fetched from url. Like this "http://gepeceper.dev/entries/1"

And I get this error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'entries.id' in 'where clause' (SQL: select * from `entries` where `entries`.`id` = 1 limit 1)

This is Entry Controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Entry;
use Session;

class EntryController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('entries.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
      $this->validate($request, array(
        'entry_name' => 'required|max:255',
        'entry_content'=>'required'
      ));

      $entry = new Entry;
      $entry-> entry_name = $request->entry_name;
      $entry-> entry_content = $request->entry_content;

      $entry->save();

      Session::flash('success', 'Entry goşmak, üstünlük bilen tamamlandy!');

      return redirect()->route('entries.show', $entry->entry_id);

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($entry_id)
    {
        $entry = Entry::find($entry_id);
        return view('entries.show')->with('entry',$entry);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

This is Table:

CREATE TABLE `entries` (
  `entry_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `entry_name` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
  `entry_content` text COLLATE utf8mb4_unicode_ci NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`entry_id`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

This is route:list: (https://photos.app.goo.gl/2PesBzTq0pd8j6B43)

0 likes
3 replies
VijayKadiyam's avatar

instead of 'entry_id' , name the column as 'id'

or

use the where clause

$entry = Entry::where('entry_id', '=', $entry_id)->first();
1 like
tykus's avatar
tykus
Best Answer
Level 104

You have called the primary key field entry_id, so you will need to update your Model by including the following line:

protected $primaryKey = 'entry_id';

By convention, Laravel expects the PK to be id, you can choose to use non-conventional PK's but must then explicitly specify this in the relevant Model

1 like
bayramgeldi's avatar

Thank for your help. It helped. You guys are fast

Please or to participate in this conversation.