Back to Error Center

Type error / Argument must be of type

Error Message

TypeError: App\Http\Controllers\PostController::show(): Argument #1 ($post) must be of type App\Models\Post, string given

Cause

A method expects a specific type but received a different one. This commonly happens with route model binding when the route parameter doesn't resolve to a model.

Solution

Verify the type hint matches the expected parameter. Ensure route model binding is properly configured. Check the method signature against how it\'s called.

Code Example

Fix
// Ensure correct type hint
public function show(Post $post): View
{
    return view('posts.show', compact('post'));
}

// If you need a string parameter instead
public function search(Request $request)
{
    $query = $request->input('q'); // String from request
    return Post::where('title', 'LIKE', "%{$query}%")->get();
}

Debugging Steps

  1. Check the method's parameter type hints
  2. Verify how the method is called
  3. Check route parameters and their types
  4. Use dd() to inspect the actual value being passed

Prevention

Always use type hints. Ensure route parameters match the type-hinted model. Test edge cases where parameters might be missing or wrong type.