© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
FilamentF
Filament•3y ago•
3 replies
tjodalv

How to update form field from resource Edit page's method?

I have resource edit form. In one placeholder field I am including my custom Livewire component. My Livewire component dispatches event 'update-stock' and with event it is sending $productId and $stock info. I am listening for that event on my resource edit page and when method is triggered I want to update stock field in the underlying form. But for some reason that stock variable doesn't change.

This is code from my resource edit page:
// app/Filament/Resources/TaskResource/Pages/EditTask.php
...
protected $listeners = ['update-stock' => 'updateProductStock'];

public function updateProductStock($args)
{
  $productId = $args['product_id'];
  $stock = $args['stock'];
  // $args are array holding 'productId' and 'stock' key
  // here I want to update field that is inside of the repeater field
  // repeater field is called 'taskItems' and every repeater item
  // has fields 'product_id' and 'stock'. This is what I was trying to do:
  $items = $this->data['taskItems'];

  foreach($items as $item) {
    if ($item['product_id'] == $productId) {
      $item['stock'] = $stock;
      break;
    }
  }
}
...
// app/Filament/Resources/TaskResource/Pages/EditTask.php
...
protected $listeners = ['update-stock' => 'updateProductStock'];

public function updateProductStock($args)
{
  $productId = $args['product_id'];
  $stock = $args['stock'];
  // $args are array holding 'productId' and 'stock' key
  // here I want to update field that is inside of the repeater field
  // repeater field is called 'taskItems' and every repeater item
  // has fields 'product_id' and 'stock'. This is what I was trying to do:
  $items = $this->data['taskItems'];

  foreach($items as $item) {
    if ($item['product_id'] == $productId) {
      $item['stock'] = $stock;
      break;
    }
  }
}
...

As you can see I was trying to manipulate field values directly in $data array, but that doesn't work. When form is rendered I still see old stock value in the form.
Solution
After some more debugging problem was the way I was updating the value. This is foreach loop that made everything working fine:

$items = $this->data['taskItems'];

foreach($items as $key => $item) {
    if ($item['product_id'] == $productId) {
        // THIS IS HOW TO UPDATE PROPERTY INSIDE DATA ARRAY
        $this->data['taskItems'][$key]['stock'] = $stock;
        break;
    }
}
$items = $this->data['taskItems'];

foreach($items as $key => $item) {
    if ($item['product_id'] == $productId) {
        // THIS IS HOW TO UPDATE PROPERTY INSIDE DATA ARRAY
        $this->data['taskItems'][$key]['stock'] = $stock;
        break;
    }
}

I think that has nothing to do with filament but rather how PHP works.
Jump to solution
Filament banner
FilamentJoin
A powerful open source UI framework for Laravel • Build and ship admin panels & apps fast with Livewire
20,307Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Update form field from page action
FilamentFFilament / ❓┊help
3y ago
Update a form field when a notification is received from Edit page
FilamentFFilament / ❓┊help
2y ago
custom page form from resource
FilamentFFilament / ❓┊help
11mo ago
Show image from url in the edit resource page form
FilamentFFilament / ❓┊help
3y ago