vendredi 15 mai 2020

Laravel: Casting an array and pushing to it?

I have a model where I have added

protected $casts = [
    'completed_steps' => 'array'
];

My schema is:

     Schema::create('runs_accomplished', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('runner_id');
        $table->json('completed_steps')->nullable();
        $table->timestamps();
    });

So I am doing this in my controller to grab (if one exists already) or create an entry:

    $runner = RunsAccomplished::where('runner_id', '=', runner_id())->first();

    if ($runner) {
        $runner->weight = $request->input('weight')
        $runner->save();
    } else {
        $runner = new \App\Models\RunsAccomplished;
        $runner->shop_name = shop_id();
        $runner->weight = $request->input('weight')
        $runner->save();
    }

How would I do both: 1) add a value to a new entry and more important, push a value to the existing array without overriding the values inside?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire