Php 8 error Cannot use positional argument after named argument

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

PHP 8 Error: Mastering Named Arguments in Laravel Development

As developers evolve with PHP, new language features are introduced to make code cleaner, more readable, and less error-prone. Among the most significant recent changes in PHP 8.x is the introduction of Named Arguments. While these features significantly enhance how we call functions and pass data, they can introduce confusion if you are not familiar with the distinction between positional and named arguments.

This post dives deep into the specific error you are encountering—"Cannot use positional argument after named argument"—and provides a comprehensive solution tailored for Laravel developers working with Eloquent and Blade views.


Understanding the PHP 8 Argument Shift

The error message arises because PHP 8 introduced Named Arguments, which allow you to explicitly name the parameter you are passing a value to, regardless of its position. This is excellent for clarity, especially in complex function calls.

Positional vs. Named Arguments

  1. Positional Arguments: These rely strictly on the order in which the arguments are passed to the function.

    function greet($name, $age) { /* ... */ }
    $result = greet('Alice', 30); // 'Alice' is $name, 30 is $age (Order matters!)
    
  2. Named Arguments: These use the parameter name to assign values. This allows you to pass arguments out of order and makes the intent of your code instantly clear.

    function greet($name, $age) { /* ... */ }
    $result = greet(age: 30, name: 'Alice'); // Order does not matter!
    

When PHP encounters a mix where you try to use a positional argument after an established named argument structure (or vice versa in complex scenarios), it throws the error because the function signature expects arguments in a specific sequence that has been violated.

Diagnosing the Laravel Context Error

Let's look at the code snippet you provided:

public function index(){
 $products = Product::all();
 return view(view:'admin.products.index', compact(varname: 'products'));
}

While this specific line might seem straightforward, the error typically surfaces when interacting with framework methods like view() or complex array/object passing in a way that PHP interprets as an invalid sequencing of arguments. The core issue here is likely how you are attempting to combine the view name and the data payload using named parameters.

The problem lies in mixing the implicit positional argument (view) with the explicit named argument (compact(...)). In modern PHP usage, if you intend to pass multiple distinct pieces of information to a function using named arguments, they must adhere to the established syntax rules.

The Correct Solution and Best Practices

To resolve this error while maintaining clean, idiomatic Laravel code, we need to structure the view() call correctly, ensuring that all parameters are explicitly defined using named arguments where appropriate.

Since the goal is to pass a view path and data context, we should ensure both parts are clearly delineated. Remember, following best practices from https://laravelcompany.com, clarity in your controller methods leads to maintainable applications.

Refactored Code Example

Instead of trying to force positional arguments into a named structure awkwardly, define the view and the data explicitly:

use Illuminate\Support\Facades\View; // Ensure you are using the correct facade if necessary

public function index()
{
    // 1. Retrieve the data using Eloquent
    $products = \App\Models\Product::all();

    // 2. Prepare the data for the view (using a standard array structure)
    $data = [
        'products' => $products,
    ];

    // 3. Pass the view and the data using clear named arguments
    return view(
        view: 'admin.products.index', // Named argument for the view path
        compact: $data             // Named argument for the data context (or pass the array directly)
    );
}

Why This Works Better

By structuring the call this way, we use named arguments (view: and compact:) exclusively. We are no longer relying on PHP's ambiguous interpretation of positional arguments following a named one. This approach is explicit and robust, preventing the "Cannot use positional argument after named argument" error entirely.

Conclusion

Mastering Named Arguments in PHP 8+ is crucial for writing modern, readable backend code. The error you faced is a common pitfall that stems from mixing these two argument styles incorrectly within function calls. By focusing on explicit naming—whether passing data to an Eloquent query or structuring a controller method call—you ensure your code remains clear and adheres to the best practices promoted by frameworks like Laravel. Always take the time to review how arguments are passed in complex scenarios to keep your application robust and easy to maintain, following the principles found at https://laravelcompany.com.