sesc360
sesc360

Reputation: 3255

Laravel passing over variables to functions

I create a GUID everytime a new database entry is being made and will be saved with the database entry to the database. The GUID should be my unique reference for further usage as reference to this entry. This part works fine.

My question: I need this GUID to perform a seach afterwards in the database for this entry, as another function needs the database entry details to work. As all the necessecary information is only created before saving to the database, how do I get a hold of this variable GUID to be used in another function?

public function createIncident(PrepareIncidentRequest $request)
{
    $incidentReference = TicketSystem::generateIncidentReference();
    $incidentID = TicketSystem::generateIncidentID();

    $data = $request->all();
    $data = $data + [
            'incident_reference' => $incidentReference,
            'incident_id'        => $incidentID,
        ];
    $incident = Incident::create($data);
    Auth::user()->incidents()->save($incident);
}

When the entry is saved, and for example this would happen 100times in an hour, how do I know what to search for in another function? So how do I "pass over" this GUID I created in the function above to be used in another function?

For example here: I want to search for a user, and all the info needed is in a specific database entry. This entry could be found with the GUID I created. But as soon as it is saved, I need a way how to know, what to search for in another function when I want to retrieve the info.

Upvotes: 0

Views: 101

Answers (1)

Margus Pala
Margus Pala

Reputation: 8663

If you need to persist data between the page views then session is the way to do it.

Add data to session like this

Session::put('key', 'value');

And read like that

Session::get('key');

Upvotes: 1

Related Questions