Dark Cyber
Dark Cyber

Reputation: 2231

Delete record error with custom method model laravel

User model

public function take($id){
    return $this->find($id);
}

public function kill(){
    return $this->delete();
}

Route Error 1

Route::get('delete/{userid}', function($id)
{

    $user = new User;
    $user->take($id); //result the content of $id
    $user->kill();
});

i can't delete record with these route and only show blank page (without error).

Route errror 2

Route::get('delete/{userid}', function($id)
{
    User::take($id)->kill();
});

And with above route i get error Non-static method User::take() should not be called statically

But i can delete with this route

Route::get('show/{userid}', function($id)
{
    $user = new User;
    $user->take($id)->kill();
});
  1. So how to fix Route error 1, if i want to use $user-> without chain take() and kill() ? if it possible
  2. How to fix Route error 2, if i want to use User:: , and why these error happen ?

Thanks in advance.

Upvotes: 1

Views: 409

Answers (1)

Gayan Hewa
Gayan Hewa

Reputation: 2397

Try below :

  Route::get('show/{$id}', function($id)
    {
     $user = new User;
     $user->find($id)->kill();
    });

I think the param accepted has to have the same things that is passed to the closure.

Upvotes: 1

Related Questions