Too few arguments to function, 1 passed and exactly 2 expected "

0

Hello apparently I have an error in my collection controller which already tried everything but still can not find the error, if they could help me it would be a good gesture: /. I am trying to create a collection of an employee so I need your id to identify the user from whom I will create your collection and the other data that appear in my method.

When I register this error appears

public function formCreate () {         return view ('caja1.crear-collect');     }

public function registrarCobranza(Request $request, $id){

    $cobranza = cobranza::find($id);
    $cobranza->usuario_id=$request-> usuario_id;
    $cobranza->monto = $request-> monto;
    $cobranza->fecha =  $request-> fecha;
    $cobranza->estatus = $request-> estatus;
      $cobranza-> save();
      $cobranza = cobranza ::all();


      return view('caja1.cobranza',compact('cobranza'));

      return redirect('cobranza');
}

Maybe I have help on my routes or because 2 waiting?

    
asked by Jorge Julian 27.10.2018 в 09:32
source

2 answers

0

The error message is clear:

  

Too few arguments to function, 1 passed and exactly 2 expected "

What it means:

  

Very few arguments for the function, 1 was passed but exactly 2 are expected

If you look at the statement of your function, you really expect two arguments:

public function registrarCobranza(Request $request, $id) { /** ... */ }

I imagine that you are calling that method from your routes and that the variable $id is a parameter of Route (Route param) . Then if your route is defined as follows:

Route::post('cobranzas/{id}/registrar', 'CobranzasController@registrarCobranza');

You can access the value of that parameter dynamically without needing to include it in the declaration of the function:

public function registrarCobranza(Request $request)
{
    $id = $request->id; // accediendo dinámicamente al valor.
    // ...
}

Plus

In fact, you can even use the Linking from model to route (Route model binding) . The documentation states:

  

Implicit link - Implicit binding

     

Laravel automatically solves the Eloquent models that are   defined in paths or controller actions whose parameter matches   with the name of a segment of the route. For example:

Route::get('api/users/{user}', function (App\User $user) {
    return $user->email;
});
     

Since variable $user is required as type of App\User and   the name of the variable matches the segment URI {user} ,   Laravel will automatically inject the model instance that   corresponds to the value of the URI request. If the   model in the database, an HTTP 404 response will be generated   automatically.

Applying the above, if you have your route defined in the following way:

Route::post('cobranzas/{cobranza}/registrar', 'CobranzasController@registrarCobranza');

As you can see, the path parameter {cobranza} that will receive the billing id has been defined. Laravel will detect this and will try to solve it with the existing models, injecting to the controller the model already found (equivalent to Cobranza::find($id) .

Then your function can be declared as follows:

/** Nota que estamos inyectando el modelo resuelto en la firma del método */
public function registrarCobranza(Request $request, Cobranza $cobranza)
{
    $cobranza->usuario_id = $request-> usuario_id;
    $cobranza->monto = $request-> monto;
    $cobranza->fecha =  $request-> fecha;
    $cobranza->estatus = $request-> estatus;
    $cobranza-> save();

    $cobranzas = cobranza ::all();

    return view('caja1.cobranza',compact('cobranzas'));
}
    
answered by 27.10.2018 в 14:37
0

In fact, the code would be more refactorized with a Custom Request where you explicitly define the fields that the user can give value from the form.

public function registrarCobranza(StoreRequest $r, Cobranza $c)
{
  $c->update($r->validated());
  return view('caja1.cobranza', [ 'cobranzas' => $c::all() ]);
}
    
answered by 28.10.2018 в 13:32