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'));
}