Too few arguments to function App\Http\Controllers\DataPegawaiController::storeDetail(), 1 passed in

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Solving Argument Mismatches in Laravel: Mastering Route Parameters and Controller Input

As a senior developer working with the Laravel ecosystem, you frequently encounter issues related to how data flows between routes, controller methods, and input requests. One of the most common stumbling blocks is exactly what you encountered: "Too few arguments to function... X passed in." This error signals a mismatch between the parameters defined in your route and the parameters expected by your controller method.

This post will dive deep into the specific issue you are facing—handling related data submission—and provide a comprehensive solution using best practices for routing, input handling, and Eloquent operations in Laravel.

The Problem: Mismatched Arguments in Data Submission

You are attempting to use a route parameter ({id}) to fetch a parent record (like a User) and then use that ID to store related child records (Pendidikan and PengalamanKerja).

The error arises because, even though your route defines an {id}, the way Laravel maps this argument to your controller method signature might be confusing the system, or perhaps you are expecting more complex data binding than a simple integer ID.

Let's analyze your setup:

Route:

Route::post('/data-pegawai/{id}/store-detail/', [DataPegawaiController::class,'storeDetail'])

Controller Method Signature:

public function storeDetail($id, Request $request) // Expects 2 arguments

When Laravel processes this, it should map the {id} from the URL to $id and the request data to $request. If you are seeing an error stating only 1 argument was passed when 2 were expected, it often means that the framework is failing to correctly resolve the positional arguments derived from the route definition, especially when dealing with nested parameters or specific route naming conventions.

The Solution: Ensuring Correct Route Mapping and Data Retrieval

The key to solving this lies in ensuring that you correctly retrieve the necessary parent data inside the controller method using the provided $id. You don't need to pass the entire user object via the URL; passing the foreign key is sufficient, and we must ensure our retrieval logic is sound.

Step 1: Refine the Controller Logic

Instead of trying to find the user inside storeDetail (which requires an extra database query), we should rely on the ID passed by the route and fetch the necessary model immediately. This separation makes the controller cleaner and adheres to the principle of Single Responsibility.

Here is how you can refactor your storeDetail method:

use App\Models\User;
use App\Models\Pendidikan;
use App\Models\PengalamanKerja;
use Illuminate\Http\Request;

class DataPegawaiController extends Controller
{
    public function storeDetail($id, Request $request)
    {
        // 1. Validate the ID exists (Crucial step for robust applications)
        $user = User::findOrFail($id); // Use findOrFail to throw a 404 error if the user doesn't exist.

        // 2. Process Education Data
        $pendidikanAttribute = [];
        $pendidikanAttribute['nama_sekolah'] = $request->input('nama_sekolah');
        $pendidikanAttribute['jurusan'] = $request->input('jurusan');
        $pendidikanAttribute['tahun_masuk'] = $request->input('tahun_masuk');
        $pendidikanAttribute['tahun_lulus'] = $request->input('tahun_lulus');
        $pendidikanAttribute['id_pegawai'] = $user->id_pegawai; // Use the retrieved ID

        Pendidikan::create($pendidikanAttribute);

        // 3. Process Work Experience Data
        $pengalamanAttribute = [];
        $pengalamanAttribute['perusahaan'] = $request->input('perusahaan');
        $pengalamanAttribute['jabatan'] = $request->input('jabatan');
        $pengalamanAttribute['tahun'] = $request->input('tahun');
        $pengalamanAttribute['keterangan'] = $request->input('keterangan');
        $pengalamanAttribute['id_pegawai'] = $user->id_pegawai; // Link back to the user

        PengalamanKerja::create($pengalamanAttribute);

        // 4. Redirect
        return redirect()->route('data-pegawai.index')->with('success', 'Detail data berhasil disimpan.');
    }
}

Step 2: Ensure Route Naming Consistency

The error often stems from how you call the route in your view. Make sure your route name (data-pegawai.storeDetail) is correctly defined and referenced everywhere. This adheres to the principles of clean, maintainable code advocated by Laravel. For more advanced routing concepts, understanding how Laravel handles routes and model binding is essential; for deep dives into framework architecture, resources like those found on laravelcompany.com provide excellent context.

Best Practices Summary

  1. Use findOrFail(): Always use methods like findOrFail($id) instead of find($id) when retrieving data based on a route parameter in controller actions. This immediately handles the case where the requested ID does not exist, preventing potential errors later on.
  2. Leverage Request Helpers: Use $request->input('key') or the more expressive $request->only([...]) to safely retrieve POST data from the request.
  3. Separate Concerns: Keep your controller focused purely on handling HTTP requests and delegating data manipulation to Eloquent models.

Conclusion

The error you faced is a classic symptom of a routing configuration issue, even when the intention seems logical. By strictly adhering to how Laravel maps route segments to method arguments—ensuring that $id from the URL correctly maps to the first parameter in your controller function—you resolve the argument mismatch. Refactoring your data access to use findOrFail() and safe input retrieval will make your application more robust, readable, and scalable. Happy coding!