Why Your Laravel Delete Fails on String IDs
You are encountering a Call to a member function delete() on a non-object error because you are attempting to find and delete a record using its name as an integer ID. In your Blade template, the link passes $category->name directly into the route parameter:
<a href="{{URL::to('/deletecat/'.$category->name) }}">...
Your route definition expects this value:
Route::get('/deletecat/{name}','CategoryController@delete');
When the controller method executes Category::find($name), Laravel treats $name as an integer primary key. However, since you passed a string (e.g., "electronics" or "clothing"), the query builder interprets this as searching for a row where the primary key column equals that specific string. Since your primary key is almost certainly an auto-incrementing integer and not a string matching the category name, find() returns null. Attempting to call delete() on null triggers the fatal error you are seeing.
To resolve this, you must ensure the route receives an integer ID rather than a descriptive name. There are two primary approaches depending on your database schema and design preferences.
Using Numeric IDs
The most robust solution for deletion operations is to store the numeric ID in the URL. If your categories table has an id column, update your Blade template to display that specific value:
<td class="center">
<a href="{{ URL::to('/deletecat/' . $category->id) }}">
<span class="glyphicon glyphicon-trash"></span>
</a>
</td>
Ensure your controller method is typed to expect an integer or cast it explicitly:
public function delete($id)
{
// Ensure we are dealing with an integer ID
$category = Category::find($id);
if ($category) {
$category->delete();
return Redirect::route('managecategory');
}
// Handle the case where the category doesn't exist
return Redirect::route('managecategory')
->withInput()
->withErrors(['error' => 'Category not found.']);
}
This pattern is standard practice because it decouples the deletion logic from the human-readable name of the entity. It also prevents potential SQL injection vulnerabilities if you were to ever use raw queries with user input directly interpolated into SQL statements, although Laravel's ORM protects against most common pitfalls.
Using Slugs Instead of Names
If your application requires users to access categories by their names or slugs (e.g., /deletecat/electronics), you should not delete based on the name alone. Deleting by string value is fragile because names are mutable. If an admin renames "Electronics" to "Consumer Electronics," any URL pointing to the old name will break, and your deletion logic might accidentally target a different row if you have duplicates or if the database allows it.
Instead, store a unique slug in your categories table that remains constant regardless of the display name. Update your model to query by this unique identifier:
public function delete($slug)
{
$category = Category::where('slug', $slug)->first();
if ($category) {
$category->delete();
return Redirect::route('managecategory');
}
return Redirect::route('managecategory')->withErrors(['error' => 'Category not found.']);
}
Security and Validation Considerations
Regardless of the approach you choose, always validate that the record exists before attempting to delete it. Calling delete() on a model instance returned by find() is safe because the instance holds the primary key; however, if you are using raw queries or eager loading, you must verify the existence first to avoid logic errors.
Additionally, consider adding confirmation steps for destructive actions. Currently, your link allows a user to delete data with a single click. In production environments, it is often safer to use a form with a CSRF token and a confirmation message rather than a simple GET request that triggers deletion immediately. Laravel's route('deletecat', ['id' => $category->id]) works fine for prototypes, but moving toward POST requests for state-changing actions aligns better with RESTful conventions and security best practices.