jeudi 4 février 2021

Laravel: Using an instance created in one function within another?

I have a User controller where set Data is run at a certain point and UserProduct's create method is used to create a UserProduct. I want to use the UserProduct in a different function. Would the best way to do so just query the latest record of UserProduct and set it to a variable?

use App\Models\UserProduct;

public function setData($data) {
    // other logic
    UserProduct::create([
       'id' => $data->id
    ]);

}

public function logInfo() {
   
    $myVar = UserProduct::latest()->first();
 
    //use myVar

}


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire