'laravel create not send data

Basically when i use

keycodes::create($data);

nothing happens and by nothing i mean nothing gets created

Model:

use HasFactory;

protected $table = 'keycodes';
protected $fillable = ['key', 'plan'];

controller:

$key = Random::generate(25, '123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ');
    return substr(chunk_split($key, 5, '-'), 0, 15);

    $data = array(
    'key' => substr(chunk_split($key, 5, '-'), 0, 15),
    'plan' => 'Basic'
    );
    keycodes::create($data);

migration:

$table->id();
        $table->timestamps();
        $table->char('key')->unique();
        $table->string('plan');
        $table->boolean('isUsed')->default(false);


Solution 1:[1]

As brombeer already said, you are already returning a value from your code on the second line return substr(chunk_split($key, 5, '-'), 0, 15);, therefore the rest of the code is not reached and thus, the last line keycodes::create($data); is actually never executed.

Your code should simply be

$key = Random::generate(25, '123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ');
$data = array(
    'key' => substr(chunk_split($key, 5, '-'), 0, 15),
    'plan' => 'Basic'
);
keycodes::create($data);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Chuck Vose