当用户单击链接时,我正在尝试使用列 SongID
从数据库中获取特定数据,但出现此错误:
SQLSTATE [42S22]:找不到列:1054 ‘where 子句’中的未知列 ‘id’(SQL:select * from
songs
whereid
= 5 限制 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');
});
}
原文由 Tartar 发布,翻译遵循 CC BY-SA 4.0 许可协议
当您使用
find()
时,它会自动假定您的主键列将是id
。为了使其正常工作,您应该在模型中设置主键。所以在
Song.php
,在类中,添加行…如果有任何可能更改您的架构,我强烈建议您将所有主键列命名为
id
,这是 Laravel 的假设,并且可能会让您免于遇到更多麻烦。