php SQLSTATE[42S22]:未找到列:1054 未知列“id”在“where 子句”中(SQL:select * from `songs` where `id` = 5 limit 1)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29347253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: select * from `songs` where `id` = 5 limit 1)
提问by Tartar
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:
SongID
当用户单击链接时,我试图通过使用列从数据库中获取特定数据,但出现此错误:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: select * from
songs
whereid
= 5 limit 1)
SQLSTATE[42S22]:未找到列:1054 'where 子句'中的未知列 'id'(SQL:select * from
songs
whereid
= 5 limit 1)
The Controller Class:
控制器类:
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use DB;
class SongsController extends Controller {
public function index()
{
$name = $this->getName();
$songs = DB::table('songs')->get();
return view('songs.index', compact('songs','name'));
}
public function show($id)
{
$name = $this->getName();
$song = DB::table('songs')->find($id);
return view('songs.show', compact('song','name'));
}
private function getName()
{
$name = 'Tupac Amaru Shakur';
return $name;
}
}
Migration:
移民:
public function up()
{
Schema::create('songs', function($table)
{
$table->increments('SongID');
$table->string('SongTitle')->index();
$table->string('Lyrics')->nullable();
$table->timestamp('created_at');
});
}
回答by user1669496
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.
当您使用时find()
,它会自动假定您的主键列将是id
。为了使其正常工作,您应该在模型中设置主键。
So in Song.php
, within the class, add the line...
因此Song.php
,在类中,添加行...
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.
如果有任何可能更改您的架构,我强烈建议您命名所有主键列id
,这是 Laravel 假设的,并且可能会让您免于更多的麻烦。
回答by Valik Gubenko
$song = DB::table('songs')->find($id);
here you use method find($id)
在这里你使用方法 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()
对于 Laravel,如果你使用这个方法,你应该有名为 'id' 的列并将其设置为主键,这样你就可以使用方法 find()
otherwise use where('SongID', $id)
instead of find($id)
否则使用where('SongID', $id)
代替find($id)
回答by Manojkiran.A
Just Go to Model file of the corresponding Controller and check the primary key filed name
只需到对应Controller的Model文件中查看主键文件名即可
such as
如
protected $primaryKey = 'info_id';
here info id is field name available in database table
这里的信息 ID 是数据库表中可用的字段名称
More info can be found at "Primary Keys" section of the docs.
更多信息可以在文档的“主键”部分找到。
回答by Kario Kevo
I am running laravel 5.8 and i experienced the same problem. The solution that worked for me is as follows :
我正在运行 laravel 5.8 并且遇到了同样的问题。对我有用的解决方案如下:
- I used bigIncrements('id') to define my primary key.
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(); });
- 我使用 bigIncrements('id') 来定义我的主键。
我使用 unsignedBigInteger('user_id') 来定义外部引用的键。
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.
我希望这会有所帮助。
回答by GERARD KANDAGOR
protected $primaryKey = 'SongID';
protected $primaryKey = 'SongID';
After adding to my model to tell the primary key because it was taking id(SongID) by default
添加到我的模型后告诉主键,因为它默认采用 id(SongID)