I'm working on Backpack and I need to save a variable that ends with the number of the last ID.
I have tried the following in the store method of my CRUD Controller, in the $last variable I want to store the value of the ID of the last record.
I have seen the last() and lastInsertId() methods but I don't know how to implement them in my solution.
<?php
public function store(StoreRequest $request)
{
$rfc = ...
$cli_rfc = ...
$date = ...
$last = Object::all()->lastInsertId();
$request->ultimo_numero =
$rfc.'-'.
$cli_rfc.'-'.
$date. '/'.
$last->id+1;
$request->request->set('ultimo_numero',$request->ultimo_numero);
// your additional operations before save here
$redirect_location = parent::storeCrud($request);
// your additional operations after save here
// use $this->data['entry'] or $this->crud->entry
return $redirect_location;
}
I don't know about backpack, but with Eloquent you can get the most recent record and set a variable equal to that value like so:
Pass
latest()
the column as an argument to the methodid
so that it is by this that you order descending and thus obtain the last record made.You chain the method
first
to only return the first recordBackpack offers you the new model generated by calling either of these two variables
In both cases it just points to id