I have this little annoying problem trying to delete a row in a paginated grid in Laravel, which worked fine a few days ago, and NOW, i'm getting this problem in all my grids whenever I try to delete any record. Here's an example of the error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.id'
in 'where clause' (SQL: delete from `users` where `id` = 9)
Now my actual code:
Migration up()
Schema::create('users', function(Blueprint $table){
$table->increments('id');
$table->string('username', 50);
$table->string('password', 50);
$table->string('email', 50);
$table->tinyInteger('roles_id');
$table->string('remember_token', 255);
$table->timestamps();
});
View
{{ Form::open(array('action' => array('UserController@delete_user'), 'method' => 'delete')) }}
{{ Form::submit('Delete', array('class'=>'btn btn-danger', 'title'=>'Delete', 'style'=>'width:70px;height:35px;margin-left:100px;')) }}
{{ Form::hidden('id','',array('id'=>'identifier')) }}
{{ Form::close() }}
Route
Route::delete('users/delete', array('uses'=>'UserController@delete_user'))->before('auth');
Controller UserController
public function delete_destroy(){
$id = Input::get('id');
User::findOrFail($id)->delete();
return Redirect::route('users');
}
Model User
class User extends Eloquent implements UserInterface, RemindableInterface {
protected $table = 'users';
protected $hidden = array('password');
protected $fillable = array('username', 'email', 'password', 'roles_id');
public static $accessible = array('username', 'email', 'roles_id');
private static function rules(){
return array(
'username' => 'required|min:6|max:20|unique:users,username,"'.Input::get('id').'"',
'password' => 'required|min:8',
'email' => 'required|email',
'roles_id' => 'required|numeric|min:1',
);
}
//Bunch of other native functions
}
That's pretty much how all my routes, views, models and controllers look.
I'm only having trouble with deleting records though, creating and updating works fine. Delete was working fine up until a few days ago, when I last deleted a record. Today I was trying something different in a view, going from create to update, then when I tried to delete it, the previously quoted error message came up.
I found out the cause of the errors. Turns out I had triggers for 4 tables, which went something like this. The following example is just for the users table though:
CREATE TRIGGER `delete_privileges` AFTER DELETE ON `users` FOR EACH ROW
BEGIN
DELETE FROM users_controllers_actions
WHERE users_controllers_actions.users_id=users.id;
END;
Guess that wasn't the right syntax... Maybe DELETED.id or OLD.id... I'm not used to using triggers in MySQL, but in SQL Server.
I'm not doing what I was supposed to do by using Foreign Key Constraints... which I didn't set at first because it gave me syntax errors. However, if anyone could tell me what the right syntax would be for creating triggers like the one I was obviously trying to create, I'd appreciate it.