Laravel Show Resource searching wrong column [closed]

醉酒当歌 提交于 2021-02-10 14:27:52

问题


I have created a model and controller for my spaces (properties) which has the database structure of; space_id, space_address, space_owner, space_price etc...

However when I visit localhost:8000/project/space/1 I am getting this error:

Column not found: 1054 Unknown column 'spaces.id' in 'where clause' (SQL: select * from spaces where spaces.id = 3 limit 1)

Function:

public function show($id)
{
        $space = Space::find($id);
        return view('space.show')->with('space', $space);

}

This is my model if this helps:

class Space extends Model
{
    protected $table = 'spaces';
    public $primarykey = 'space_id';
}

回答1:


you should use

$primaryKey = 'space_id';

instead of :

$primarykey = 'space_id';

Notice that the case sensitivity for the variables names in PHP:

Variables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive.




回答2:


In your table there is space_id but Eloquent query looking for id column. You must update column name space_id to id then it will work.




回答3:


you should correctly set your primary key in your model:

protected $primaryKey = 'space_id';


来源:https://stackoverflow.com/questions/55571898/laravel-show-resource-searching-wrong-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!