Karl_T Posted May 12, 2017 Share Posted May 12, 2017 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 More sharing options...
Robin S Posted May 12, 2017 Share Posted May 12, 2017 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. 2 Link to comment Share on other sites More sharing options...
Karl_T Posted May 12, 2017 Author Share Posted May 12, 2017 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now