R.S Namdhari
R.S Namdhari

Reputation: 93

Laravel Controller function not returning row with primary key

I have a problem in my resource controller. When I am try to get the row to delete record with destroy function like I described below it does not work.

public function destroy(SubCategory $subCategory)
{

$subCategory->delete();

}

but if I try some thing like below, it works

public function destroy($subCategory)
{

SubCategory::find($subCategory)->delete();

}

I just want to do that with the first one, I do not know why it's not working?

Upvotes: 2

Views: 145

Answers (2)

R.S Namdhari
R.S Namdhari

Reputation: 93

I found an error in my web.php file.It's my mistake I define my resource route like that

  Route::resource('subcategory', 'SubCategoryController');

but it should be like below

Route::resource('subCategory', 'SubCategoryController');

Upvotes: 0

num8er
num8er

Reputation: 19372

Try to define Your model in RouteServiceProvider or directly on routes.php file before resource directive:

public function boot(Router $router)
{
    parent::boot($router);

    Route::model('subCategory', App\SubCategory::class);
}

route service provider example

read about: explicit binding

Upvotes: 1

Related Questions