-
Notifications
You must be signed in to change notification settings - Fork 42
Docs ‐ Record
USSD record provides a place to save data during USSD execution. You can inject in any USSD state or action where you need to persist data or to retrieved already saved data.
<?php
namespace App\Ussd\States;
use Speso\Ussd\Contracts\State;
use Speso\Ussd\Menu;
use Speso\Ussd\Record;
class SimpleState implements State
{
public function render(Record $record): Menu
{
$name = 'John Doe';
$record->set('name', $name);
$age = $record->get('age');
return Menu::build()->format('%s is %d years old.', $name, $age);
}
}Available Methods
$record->set('name', 'John Doe');
$record->get('age', 13);
$record->setMany(['name' => 'John Doe', 'age' => 13]);
[$name, $age] = $record->getMany(['name', 'age']);
$record->forget('age');
$record->forgetMany(['name', 'age']);
$record->increment('age');
$record->decrement('age');
$record->has('name');
$name = $record('name'); // to get value
$name = $record->name; // to get value
$record(['age' => 17]); // to set values
isset($record->name); // to check valueAll data saved with records are only available within a USSD session. Once the session ends, all the data can not be retrieved any more. If you will like to save data across sessions for a particular user, set the public property of the method to true.
$record->set('name', 'John Doe', public: true);If you would want to data to be deleted after some time, set the ttl value;
$record->set('name', 'John Doe', ttl: now()->addMinute());Record::setLocale() and Record::locale() persist a session's chosen language across requests.
$record->setLocale('fr');
$record->locale(); // 'fr'Once set, Ussd applies it automatically on every following request in that session — no need to call App::setLocale() yourself in every state. For a session that hasn't called setLocale(), the locale resets to your application's configured default (config('app.locale')) on every request, so one session's language choice never leaks into another's on long-running processes (Octane, queue workers). See Docs ‐ Menu for building translated menu content.
Record::set() stores values as-is in your configured cache store. For sensitive values — PINs, account numbers — use setEncrypted()/getEncrypted() instead, which encrypt with your application's key before writing and decrypt on read, so the raw cache backend never holds plaintext:
$record->setEncrypted('pin', $pin);
$record->getEncrypted('pin'); // decrypted valueBoth accept the same ttl and public parameters as set()/get().