I've seen similar questions for other frameworks but I cant seem to find an answer for Laravel.
What is the best practise / cleanest solutions to achieve my goal.
At the moment lets say I have a model called Site
.
My site model has a column called prefix
which is just a few capital letters generated by the site name plus the new site id so for example, row 1 with a site name of 'clare' becomes 'CLA1' and 'pencet' becomes 'PEN2'.
My issue is that I need the new row id
to generate the prefix, which means at the moment I have to save it, then re-save it like so.
$site = new Sites;
$site->name = 'clare';
$site->save();
//Now that we have saved, we can use the new id to create the prefix and save again
$site->prefix = strtoupper(substr($site, 0, 3)) . $site->id;
$site->save();
I hate the fact I have to re-save it again once its initially been saved because I need the id, what is the best way to achieve this goal? Is it possible to return the new model ID before it saves?
I know I can do stuff like get a count of all of the rows and get the ID that way instead, but i'm generally looking for the best, cleanest solutions to achieve this.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire