问题
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
wherespaces
.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