-
I'm a little confused about the usage of repeaters. <a17-fieldset title="Videos" id="videos" :open="true">
@formField('repeater', ['type' => 'video'])
</a17-fieldset> This seems to use the repeaters without the Block Editor, but I can't figure out how to make it work, where should Any help appreciated, Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 16 comments 1 reply
-
Hi @AmrNN, Yes, the repeater could be used outside of the Block Editor. However, different from using it as a block, we have to create a module for it in this case. Take the code snippet in your comment as example, let's assume you are working on an module named "Post", after declaring the formField in view, you will want to create a corresponding "video" module using the command Next, what we are going to do is to define the one-to-many relationships between the original module[ex. Post] and repeater module [ex. Video]. Make a new migration to create an new column public function video()
{
return $this->hasMany(\App\Models\Video::class);
} Now, go to the repository of the module which you are working on, in this case, maybe public function afterSave($object, $fields)
{
$this->updateRepeater($object, $fields, 'video', 'Video');
parent::afterSave($object, $fields);
} public function getFormFields($object)
{
$fields = parent::getFormFields($object);
$fields = $this->getFormFieldsForRepeater($object, $fields, 'video', 'Video');
return $fields;
} Checkout more details about the custom form method in documentation There still have some tricky parts I didn't addressed out here, but the repeater as static content should be able to work. I would suggest to read the source code if you met problems while doing this, but let me know if you have further questions, I'm always happy to help! |
Beta Was this translation helpful? Give feedback.
-
Hey @yanhao-li, thanks for the instructions. I was going to try digging into the source code since the docs seem to be missing some details but I wanted to make sure it was possible to achieve what I'm asking for first. I will try to get this working now and see what I get, thanks again. |
Beta Was this translation helpful? Give feedback.
-
It might also be valuable to mention that this type of repeater should still be defined in 'block_editor' config section as with repeaters in Block Editor. file:
|
Beta Was this translation helpful? Give feedback.
-
@zipavlin You are right! All repeaters have to be defined first in config. |
Beta Was this translation helpful? Give feedback.
-
@yanhao-li I was just a bit confused about settings for this repeater being under "block editor" as the field itself is used outside of it. Maybe it would make sense to move config for all repeaters outside of "block_editor" (this are already semantics)? |
Beta Was this translation helpful? Give feedback.
-
Hi I'm trying to create a repeater field as above, but store the data in a serialized field, rather than a related model? I'm currently using: |
Beta Was this translation helpful? Give feedback.
-
Any chance you can share this code @Riaan-ZA ? Also @yanhao-li , its very unclear to me still in what view file the fields that appear within the repeater go. Can you clarify ? Right now I have a repeater field visible and I can add/remove rows but it has no fields within it. |
Beta Was this translation helpful? Give feedback.
-
Hi @Arkid I have it working as follows:
Then in your module repository to format the data for the form:
this is the getJsonRepeater function:
Then when saving the data, use this function to assign the data from the repeater to the db field which gets json encoded by the casts functionality above:
This might not work 100% for your use case but should point you in the right direction |
Beta Was this translation helpful? Give feedback.
-
Thanks for this @Riaan-ZA , that is amazing and just what I wanted as the earlier example of saving the results into a seperate table were overkill for my scenario. The problem I now have is confusion on how views need to be configured. I have this setup in the view of the master module...
And that makes the rates field appear fine, but when I click the add new row button it appears but its empty, just a blank space with no field inputs. Where is the direction of the fields that appear inside the repeater? In another view file and what is the naming/location convention for this? |
Beta Was this translation helpful? Give feedback.
-
That sounds like you missed something in the process of creating the repeater field. Have you created the block blade template for you repeater field containing the fields you want and have you run |
Beta Was this translation helpful? Give feedback.
-
Yes exactly @Riaan-ZA thanks for your time. I found what I needed to run and wasn't was the php artisan twill:blocks command. I'm new to vue and I'm not really sure what this command and the npm one are doing so my running of these commands has been somewhat arbitrary. That mixed with having the name of the component in the twill settings not matching the blocks view file caused the issue. Thanks again |
Beta Was this translation helpful? Give feedback.
-
@yanhao-li do you know if running the npm twill build command is actually required if you are only using a repeater outside of the full block infrastructure in your twill project ? I'm somewhat confused about the requirement for this step. It's clear I need to run the twill:blocks command after any changes to my blocks but on this npm command I'm not sure. Do you have a second to clarify ? |
Beta Was this translation helpful? Give feedback.
-
Hey @Arkid , Regards to your questions,
As for the problem that you met, could you please check out the common error in documentation and make sure you did everything right? Additional to that, I'm not sure which version of Twill are you using, but there is a known issue in 1.2.0 which may cause the same problem as you met, in that case, you will need to update the
Checkout the discussion here: #151 |
Beta Was this translation helpful? Give feedback.
-
Thanks so much for taking the time to clarify this clearly @yanhao-li ;) I have this working now, it was a combination of not running the two console commands and also not knowing where the view file needs to go (ie in the /views/admin/blocks folder path). |
Beta Was this translation helpful? Give feedback.
-
How does the foreign key column get populated? I'm getting this error on save |
Beta Was this translation helpful? Give feedback.
-
just incase anyone out there is as much of a goof as me, I forgot to add the foreignkey to my fillables on the repeater module, problem solved |
Beta Was this translation helpful? Give feedback.
Hi @AmrNN,
Yes, the repeater could be used outside of the Block Editor. However, different from using it as a block, we have to create a module for it in this case.
Take the code snippet in your comment as example, let's assume you are working on an module named "Post", after declaring the formField in view, you will want to create a corresponding "video" module using the command
php artisan twill:module videos
, following the command line instructions to define the module routes and migrate the databases.Next, what we are going to do is to define the one-to-many relationships between the original module[ex. Post] and repeater module [ex. Video]. Make a new migration to create an new column