Update plugin entry via frontend

Is it possible to update plugin entries via frontend? I’m calling this onUpdate() function on frontend via {{ form_ajax('onUpdate') }} with fill function and it keeps saving new entries, instead of updating it. Should entry ID be attached to the onUpdate function, somewhere so it knows what record to update?

   public function onUpdate(){
        $tracker = new Tracker();
        $data = (array) post();
        $tracker->fill($data);
        $tracker->save();
        Flash::success('Updated!');
        return Redirect::back();
    }

One way could be

   public function onUpdate(){
        if ($_POST['tracker_id') {
            $tracker = Tracker::find($_POST['tracker_id');
            ...
        }
    }
1 Like

Does this mean that I have to laravel way to update DB record?

Db::update('update tracker set votes = 100 where name = ?', ['John']);

Isn’t there any out of the box OctoberCMS way to do this?

This was asked before here:

Not using tailor here, and can see that $tracker->fill($data); makes correct select (selects specifict record ID) statement in the debugbar, but $tracker->save(); ignores it and creates INSERT statement.

$tracker->fill($data); result:
select * from timetracker_tracker_wheretimetracker_tracker_.id = '15' limit 1

$tracker->save(); result:
insert into timetracker_tracker_ (workday_start_min) values ('02:45')

Here it should be triggered UPDATE statement, but no clue how to do this.

I don’t understand what you are saying. But I did an example with your snippet :

        $postData = ['id' => '1', 'description' => 'test'];
        $title = new Title();
        $data = $postData;
        $title->fill($data);
        $title->save();

Before executing this code, I already had a Title with ID 1 saved in my database.

Now, when I execute, I have an error saying that this record is already existing - which is totally normal;

"SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicata du champ '1' pour la clef 'PRIMARY' (SQL: insert into `PRIVATE_titles` (`id`, `description`, `updated_at`, `created_at`) values (1, test, 2024-04-12 08:36:39, 2024-04-12 08:36:39))"

If the model instance being saved does not have a primary key set (e.g., if it’s a new instance YOUR CASE DOING NEW TRACKER()), Laravel will insert a new record into the database. If the model instance has a primary key set and a record with that primary key already exists in the database, Laravel will update that existing record with the new data.

Take a look at the Laravel ORM documentation. Eloquent: Getting Started - Laravel 9.x - The PHP Framework For Web Artisans

You will find some example on how to update a record.

use App\Models\Flight;
 
$flight = Flight::find(1);
$flight->name = 'Paris to London';
$flight->save();

Thanks for any hints, I managed to solve this with AI. Attaching working example.

  public function onUpdate(){
        $id = post('record'); // Assuming the primary key is called 'id'
        $tracker = Tracker::find($id);

        if (!$tracker) {
            // Handle case where record not found
            Flash::error('Tracker not found!');
            return;
        }

        $data = (array) post();
        $tracker->fill($data);
        $tracker->save();
        Flash::success('Tracker updated!');
        return Redirect::back();
    }

You’re welcome.