Jump to content

Remove repeater item by page ID rather than item ID


Karl_T
 Share

Recommended Posts

I want to use the following code to delete a repeater item,

$item = $p->repeater->get(2011); //get the item by page ID
$p->repeater->remove($item);
$p->save();

but it fails. the get method is looking for repeater item ID started from 0.

After some testing, the repeater item ID is just like any array key. If someone using that to track an item, wrong items could be deleted/edited due to the dynamic item ID.

Also, it is quite weird that when I want to get the repeater item ID like

foreach($repeater as $item){
    return $item->id;
}

it returns the repeater page ID instead of the repeater item ID. It is actually  how I get the repeater page ID lol. What I mean weird is that the get method above is not looking for page ID while when I ask for item ID from it, it returns page ID.:(

 

So, I decided to use page ID again to move repeater item after all these trying. I am using the following code to remove repeater item.

$item = wire("pages")->get(2011);
$p->repeater->remove($item);
$p->save();

It works. However, it seems like it is not the designed way, more like a workaround. Please advise if there is any native way. Thanks.

Link to comment
Share on other sites

1 hour ago, Karl_T said:

After some testing, the repeater item ID is just like any array key.

I'm not aware of anything that is known as a repeater "item ID". When you use the get() method this is not something that is specific to repeater fields - it is simply the WireArray::get() method (because a repeater field returns a kind of WireArray). If you give it an integer argument then this is assumed to be the index of the item. But you could supply a selector instead, or an array of keys, or anything covered in the method documentation.

 

1 hour ago, Karl_T said:

Also, it is quite weird that when I want to get the repeater item ID like


foreach($repeater as $item){
    return $item->id;
}

it returns the repeater page ID instead of the repeater item ID.

Nothing weird here either - a repeater item is a Page object so of course you can get its ID property, same as any other page.

 

1 hour ago, Karl_T said:

So, I decided to use page ID again to move repeater item after all these trying. I am using the following code to remove repeater item.


$item = wire("pages")->get(2011);
$p->repeater->remove($item);
$p->save();

It works. However, it seems like it is not the designed way, more like a workaround.

That looks fine to me. The remove() method expects an object or an index argument - in your case the page object is probably most suitable. How you get the page object you want to remove is up to you - if you already know its ID then what you are doing is fine.

  • Like 2
Link to comment
Share on other sites

58 minutes ago, Robin S said:

If you give it an integer argument then this is assumed to be the index of the item

I used to use $pages->get($id) to get the page. I assumed they work the same as repeaters are pages behind the scene. My bad. :(

Thanks @Robin S again!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...