Search code examples
laravelvariablesinfyom

Set random string variable in laravel InfyOm


Hi i use InfyOm (Laravel Genrator). I want set random string to variable key on adding new record, but i dont know how i can do it.

Keys Controller

public function store(CreatekeysRequest $request)
{
    $input = $request->all();

    $keys = $this->keysRepository->create($input);


    Flash::success('Added');

    return redirect(route('keys.index'));
}

i generate string on key model with mutator but key changed always when i edit record.

Keys model

public function setDomainAttribute($value) {
    $this->attributes['domain'] = $value;
    $key = $this->attributes['key'] = Str::random(16);
    Flash::success("Key generated for {$value}<br><b>{$key}");
}    

Solution

  • I'm not familiar with InfyOm but I believe you can just use the standard Eloquent events like so:

    <?php
    // models/Key.php
    class Key
    {
        public static function boot()
        {
            parent::boot();
            self::creating(function($model) {
                $model->key = Str::random(16);
            });
        }
    }
    

    The above code will set the key property when you're creating a new Key model.

    See https://laravel.com/docs/7.x/eloquent#events