Home » Php » php – Issue with displaying randomly generated code

php – Issue with displaying randomly generated code

Posted by: admin February 25, 2020 Leave a comment

Questions:

So i have a section of code that creates a random code and sends it in a private message (on another website) to them, however id like to update it to also display the code in the success message

Models

public function createDepositRequest()
    {
        // Generate unique code
        $code = Codes::generateCode();

        $deposit = Deposits::create([
            "accountID" => $this->id,
            "code" => $code,
        ]);
        echo $code;
        dispatch(new CreateDepositRequest($deposit));
    }

UserController

/**
     * Creates a deposit request for an account
     *
     * @param int $accountID
     * @throws UserErrorException
     */
    protected function deposit(int $accountID)
    {
        // Get the account
        $account = Accounts::find($accountID);

        if ($account === null || $account->count() === 0)
            throw new UserErrorException("That account doesn't exist");

        // Verify that the submitting user owns this account
        if (Auth::user()->nID != $account->nID)
            throw new UserErrorException("You don't own that account, silly billy");

        // Now call to create the deposit request
        $account->createDepositRequest();

        $this->output->addSuccess("Deposit request added successfully. Please check your inbox in-game for instructions");
    }

Job (the thing that has it sent to the other site) “`

namespace App\Jobs;

use App\Classes\Nation;
use App\Classes\PWClient;
use App\Models\Accounts;
use App\Models\Deposits;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class CreateDepositRequest implements ShouldQueue
{
    use InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Holds the deposit request
     *
     * @var Deposits
     */
    protected $deposit;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(Deposits $deposit)
    {
        $this->deposit = $deposit;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $client = new PWClient();
        $client->login();

        // Get nation info
        $nation = new Nation($this->deposit->account->nID);

        // Setup message
        $message = "Hi {$nation->leader},\n\n You've submitted a deposit request for the account named: {$this->deposit->account->name}.\n\nThe code for your deposit is: {$this->deposit->code}\n\nPlease send whatever money and resources you want to deposit into your account into the bank using the code above as the transaction, just like how you make a payment towards a loan.\n\nPlease note that the system checks for deposits at five past the hour, so if you make a deposit during that time, your deposit [b]will not count[/b]. So please try to avoid that. Additionally, this means that your deposit will not show up in your account until then. You will receive a confirmation email when your deposit is processed. If you not get a message within two hours, please contact us.";

        // Now send them a message
        $client->sendMessage($nation->leader, "Deposit Request", $message);
        echo "{$this->deposit->code}";
    }
}

So ive tried a dozen things but im reaching a level where im above my knowlage it would appear. You can see that in the models i have it just echo the code, and since the user controller actually sends the success message, im not sure how to return the code generated in models for use in the usercontroller.

So to clarify i made a screenshot https://prnt.sc/r42p0v

Attempts tried: to add
-return $code; error was “undefined variable”

How to&Answers:

I can’t say absolutely for sure without seeing more of the code, but it looks like you should be able to do this…

In your model, after

dispatch(new CreateDepositRequest($deposit));

add

return $code;

Then in your controller, replace

$account->createDepositRequest();

with

$code = $account->createDepositRequest();

Having done that, you should be able to access the returned $code value in the next line as something like

$this->output->addSuccess("Deposit request added successfully.  Your code is $code. Please check your inbox in-game for instructions");

The only thing that would throw a wrench in this would be if there’s something going on in dispatch(new CreateDepositRequest($deposit)); that isn’t fully shown in your code above.