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

前端 未结 5 1844

I am trying to get specific data from the database by using column SongID when a user clicks a link but I am getting this error:

SQLSTATE[42

相关标签:
5条回答
  • 2021-02-02 08:14

    protected $primaryKey = 'SongID';

    After adding to my model to tell the primary key because it was taking id(SongID) by default

    0 讨论(0)
  • 2021-02-02 08:18

    I am running laravel 5.8 and i experienced the same problem. The solution that worked for me is as follows :

    1. I used bigIncrements('id') to define my primary key.
    2. I used unsignedBigInteger('user_id') to define the foreign referenced key.

          Schema::create('generals', function (Blueprint $table) {
              $table->bigIncrements('id');
              $table->string('general_name');
              $table->string('status');
              $table->timestamps();
          });
      
      
          Schema::create('categories', function (Blueprint $table) {
              $table->bigIncrements('id');
              $table->unsignedBigInteger('general_id');
              $table->foreign('general_id')->references('id')->on('generals');
              $table->string('category_name');
              $table->string('status');
              $table->timestamps();
          });
      

    I hope this helps out.

    0 讨论(0)
  • 2021-02-02 08:21
    $song = DB::table('songs')->find($id);
    

    here you use method find($id)

    for Laravel, if you use this method, you should have column named 'id' and set it as primary key, so then you'll be able to use method find()

    otherwise use where('SongID', $id) instead of find($id)

    0 讨论(0)
  • 2021-02-02 08:25

    Just Go to Model file of the corresponding Controller and check the primary key filed name

    such as

    protected $primaryKey = 'info_id';
    

    here info id is field name available in database table

    More info can be found at "Primary Keys" section of the docs.

    0 讨论(0)
  • 2021-02-02 08:27

    When you use find(), it automatically assumes your primary key column is going to be id. In order for this to work correctly, you should set your primary key in your model.

    So in Song.php, within the class, add the line...

    protected $primaryKey = 'SongID';
    

    If there is any possibility of changing your schema, I'd highly recommend naming all your primary key columns id, it's what Laravel assumes and will probably save you from more headaches down the road.

    0 讨论(0)
提交回复
热议问题