Model delete method does not erase record

Asked

Viewed 666 times

0

I have the code below on the route, but it doesn’t work! What am I doing wrong? It is returning users correctly but doesn’t delete.

$Usuario = User::find(2);
$Usuario->delete();
return User::orderBy('username', 'asc')->take(4)->get();
  • try to use a Try ali in delete to see why. An alternative is you use User::Destroy($id)

  • User::Destroy($id) didn’t work, what’s wrong? No error returns.

  • dd( DB::getQueryLog(); after $Usuario->delete(); ai you will see SQL, paste here

  • array (size=2)
 0 => 
 array (size=3)
 'query' => string 'select * from userswhereid= ? limit 1' (length=44)
 'bindings' => 
 array (size=1)
 0 => int 2
 'time' => float 0.68
 1 => 
 array (size=3)
 'query' => string 'delete fromuserswhereid is null' (length=38)
 'bindings' => 
 array (size=0)
 empty
 'time' => float 0.33

  • you’re using softdelete ?

  • I am not using softdelete, but I already did the test using it and the same error occurred!

  • put the code of your model.

  • At the moment it’s like this: <?php&#xA;&#xA;class User extends Eloquent {&#xA;&#xA; public $timestamps = false;&#xA;&#xA; /**&#xA; * The database table used by the model.&#xA; *&#xA; * @var string&#xA; */&#xA; //protected $table = 'users';&#xA;&#xA; /**&#xA; * The attributes excluded from the model's JSON form.&#xA; *&#xA; * @var array&#xA; */&#xA; protected $hidden = array('password', 'remember_token');&#xA;&#xA;}&#xA;

  • Createreadupdate working less delete than not.

Show 4 more comments

2 answers

1

Use Builder instead of ORM as a test

DB::table('users')->where('id', 2)->delete();

I think this will work

0

Add primary key setting to your model:

protected $primaryKey = 'ID';

It worked for me...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.