【问题标题】:SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: select * from `songs` where `id` = 5 limit 1)SQLSTATE [42S22]:未找到列:1054 'where 子句'中的未知列 'id'(SQL:select * from `songs` where `id` = 5 limit 1)
【发布时间】:2015-06-03 12:43:18
【问题描述】:

当用户单击链接时,我正在尝试使用列 SongID 从数据库中获取特定数据,但出现此错误:

SQLSTATE[42S22]:找不到列:1054 未知列 'id' 在 'where 子句' (SQL: select * from songs where id = 5 limit 1)

控制器类:

<?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;
    }
}

迁移:

    public function up()
    {
            Schema::create('songs', function($table)
            {
                $table->increments('SongID');
                $table->string('SongTitle')->index();
                $table->string('Lyrics')->nullable();
                $table->timestamp('created_at');
            });
    }

【问题讨论】:

  • 您的表架构是什么样的?显然,由于某种原因,您的表中似乎缺少 id 列....
  • 我的表中没有名为 id 的列,而是有 SongID

标签: php mysql laravel laravel-5


【解决方案1】:

当您使用find() 时,它会自动假定您的主键列将是id。为了使其正常工作,您应该在模型中设置主键。

所以在Song.php的类中,添加一行...

protected $primaryKey = 'SongID';

如果有可能更改您的架构,我强烈建议您将所有主键列命名为 id,这是 Laravel 的假设,并且可能会让您免于以后的更多麻烦。

【讨论】:

  • 这是极好的解决方案和最佳答案。
  • 这也适用于您在使用资源或集合时可能遇到的错误,因为 Laravel 倾向于使用相同的模型。
  • 此方案同样适用于其他出现相同错误的场景。在我的情况下,它是一个 CreateOrUpdate 语句失败,因为我没有定义主键,它默认返回检查 where 'id' is null
【解决方案2】:

到对应Controller的Model文件中查看主键文件名即可

比如

protected $primaryKey = 'info_id';

这里的 info id 是数据库表中可用的字段名称

更多信息可以在the docs的“主键”部分找到。

【讨论】:

    【解决方案3】:
    $song = DB::table('songs')->find($id);
    

    这里你使用方法find($id)

    对于 Laravel,如果你使用这种方法,你应该有一个名为 'id' 的列并将其设置为主键,这样你就可以使用方法find()

    否则使用where('SongID', $id) 而不是find($id)

    【讨论】:

    • @CJ Suspend 此语句不从数据库返回任何内容。
    • $song = DB::table('songs')-&gt;where('SongID', $id)-&gt;first() 这应该返回一个模型(只要您的数据库中有具有该 ID 的东西)。那么你应该可以通过$song-&gt;SongTitle 获得它的属性
    【解决方案4】:

    protected $primaryKey = 'SongID';

    添加到我的模型后告诉主键,因为它默认采用 id(SongID)

    【讨论】:

      【解决方案5】:

      我正在运行 laravel 5.8 并且遇到了同样的问题。对我有用的解决方案如下:

      1. 我使用 bigIncrements('id') 来定义我的主键。
      2. 我使用 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();
            });
        

      我希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-09-12
        • 2021-11-08
        • 1970-01-01
        • 2020-03-01
        • 2020-06-08
        • 1970-01-01
        • 2021-03-29
        相关资源
        最近更新 更多