Reputation: 1025
I am trying to update an existing table record with Laravel 4 and MySQL, but once I click the submit button after having entered new text for a property in the edit form I get the error message:
SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘id’ in ‘where
clause’ (SQL: update `persona` set `bio` = new bio where `id` is null)
What am I doing wrong here?
Routes.php:
Route::resource('personas', 'PersonaController');
PersonaController:
public function show($id)
{
$persona = Persona::where('idPersona', $id)->first();
return View::make('personas.show')->with('persona', $persona);
}
public function edit($id)
{
$persona = Persona::where('idPersona', $id)->first();
return View::make('personas.edit')->with('persona', $persona);
}
public function update($id)
{
$persona = Persona::where('idPersona', $id)->first();
$persona->fName = Input::get('fName');
$persona->lName = Input::get('lName');
$persona->mName = Input::get('mName');
$persona->bio = Input::get('bio');
$persona->dateBorn = Input::get('dateBorn');
$persona->dateDied = Input::get('dateDied');
$persona->save();
return Redirect::to('personas');
}
show.blade.php
<h3>ID: {{ $persona->idPersona }}</h3>
{{ link_to("/personas/{$persona->idPersona}/edit", 'Edit this persona') }}
edit.blade.php:
{{ Form::model($persona, array('route' => array('personas.update', $persona->idPersona), 'method' => 'PUT')) }}
<div>
{{ Form::label('fName', 'First Name: ') }}
{{ Form::input('text', 'fName', $persona->fName) }}
</div>
<div>
{{ Form::label('lName', 'Last Name: ') }}
{{ Form::input('text', 'lName', $persona->lName) }}
</div>
<div>
{{ Form::label('mName', 'Middle Name: ') }}
{{ Form::input('text', 'mName', $persona->mName) }}
</div>
<div>
{{ Form::label('bio', 'Bio: ') }}
{{ Form::textarea('bio', $persona->bio) }}
</div>
<div>
{{ Form::label('dateBorn', 'Birthday: ') }}
{{ Form::input('text', 'dateBorn', $persona->dateBorn) }}
</div>
<div>
{{ Form::label('dateDied', 'Deathday: ') }}
{{ Form::input('text', 'dateDied', $persona->dateDied) }}
</div>
<div>{{ Form::submit('submit edit') }}</div>
{{ Form::close() }}
MySQL Table:
CREATE TABLE `mountain`.`persona` (
`idPersona` INT NOT NULL AUTO_INCREMENT,
`fName` VARCHAR(45) NULL,
`lName` VARCHAR(45) NULL,
`mName` VARCHAR(45) NULL,
`bio` TEXT NULL,
`dateBorn` VARCHAR(45) NULL,
`dateDied` VARCHAR(45) NULL,
PRIMARY KEY (`idPersona`));
Upvotes: 0
Views: 1058
Reputation: 10603
The default primary key column defined by laravel in it's models is id
. You are loading your model's via a where
lookup, rather than by primary key, hence you are able to load the model, but when saving, laravel query builder has no idea that you aren't using the 'id' column it expects.
In your model set this:
protected $primaryKey = 'idPersona';
and now you can load your model via:
Persona::find('place id here');
Upvotes: 1