简体   繁体   中英

How can I update the data inside a model with an action in Laravel?

I have a model called 'demande', with a particular field named 'statut' with default value 'en cours'. I created an action where when the button is clicked the 'statut' field value becomes 'Validé'

demande.php(the model)

protected[ 'type' 'statut' ]

Validate.php(the action) `

public function handle($model, View $mainValidator){
        $model->update([
            'statut' => 'Validé'
        ]);
    }

` ValPcTableView.php(the livewire component where I implemented the action)

public function actionsByRow(){
    return[
        new Validate,
        new Refuse,
        new Consulter
    ];
}

It always return 'Call to a member function update() on array'

There are multiple ways of approaching this. If you already have a model here, you can simply add a parameter to it and then save it. But be sure to check if your model extends Illuminate\Database\Eloquent\Model

Your model would look something like this:

<?php
 
namespace App\Models;
 
use Illuminate\Database\Eloquent\Model;
 
class demande extends Model
{
    protected $fillable = ['type', 'statut'];
}

Your model update would look like this:

$model->statut = 'Validé';
$model->save();

https://laravel.com/docs/9.x/eloquent#updates

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM