https://twill.io logo
Join Discord
Powered by
# ❓questions
  • k

    Klief

    09/10/2021, 1:28 PM
    That is strange, I have the column but it is not displaying the sidebar one...
  • k

    Klief

    09/10/2021, 1:31 PM
    Ah, it was not in my fillable array, my bad!
  • k

    Klief

    09/10/2021, 1:31 PM
    It also displays end date though, even though it is not in my table
  • i

    ifox

    09/10/2021, 1:36 PM
    that's a bug, I think there's a github issue already
  • p

    patrick

    09/10/2021, 1:43 PM
    Good catch! I'll fix this.
  • h

    Hữu Hiếu

    09/10/2021, 2:55 PM
    when i edit the post i have to click on the x button first and then on the update button it returns the data
  • i

    ifox

    09/10/2021, 2:57 PM
    by default it is set to the current time @Hữu Hiếu
  • i

    ifox

    09/10/2021, 2:58 PM
    if it was empty
  • s

    Sami

    09/10/2021, 3:21 PM
    will try next time it happens
  • d

    Defenestrația

    09/10/2021, 3:51 PM
    How do I use the browser in a two-way fashion? I have a belongsToMany relationship between 2 models... Let's call them First and Second. When I create the form for First, I set this up: @formField('browser', [ 'moduleName' => 'seconds', 'name' => 'first_second', 'label' => 'Second', ]) On the form for Second, I set this up: @formField('browser', [ 'moduleName' => 'firsts', 'name' => 'first_second', 'label' => 'First', ]) When I link my Second to my First in the First form, it creates an entry in the "related" table with subject_type First and related_type Second. When I link my First to my Second in the Second form, it creates a second entry in the "related" table with subject_type Second and related_type First. How do I link these together so I have one relationship that can be created/edited/deleted in either form?
    p
    • 2
    • 11
  • i

    ifox

    09/10/2021, 3:54 PM
    @Defenestrația at the moment you would have to use browsers with your own pivot table instead of the HasRelated feature. But @pboivin has been working on bidirectional belongsTo recently, so this might be something he might be able to help with
  • d

    Defenestrația

    09/10/2021, 5:36 PM
    Thanks! pboivin was able to help me out in private message. 🙂
  • u

    user

    09/10/2021, 8:29 PM
    Hello, I'm making a Api Rest for my website and using Twill, I would like to know if there is a way to return the Blocks elements with the images on JSON. I'm currently doing this to get the Blocks and image:
    Copy code
    public function getContentsAttribute(){
            $blocks = [];
            foreach ($this->blocks()->orderBy('position')->get() as $block){
                $current =
                    ['type' => $block->type]
                ;
                if($block->type == 'image'){
                    $current['content'] = ['image' => $block->imagesWithCrops('image')];
                }elseif($block->type == 'galeriaImgTexto'){
                    $current['content'] = ['itens' => []];
                    foreach ($block->children as $child){
                        $currentChild = $child->content;
                        $currentChild['images'] = $child->imagesWithCrops('image');
                        $current['content']['itens'][] = $currentChild;
                    }
                }
                else{
                    $current['content'] = $block->content;
                }
                $blocks[]=$current;
            }
            return $blocks;
        }
  • p

    pboivin

    09/10/2021, 8:48 PM
    Hi @User , this looks fine. Does it work for what you need? Or are you looking for improvements?
  • u

    user

    09/10/2021, 8:50 PM
    Helo @User , it does work, but i'm looking for improvements. I want to know if there is a way to return the imagens in the block automatically. I have currently 5 types of block, the elseif is getting bigger and bigger
  • p

    pboivin

    09/10/2021, 9:01 PM
    Ok, that's interesting! You could use
    $blocks->media
    to query all
    Media
    items attached to your blocks, but this may not be exactly what you need. It's a bit low level...
  • p

    pboivin

    09/10/2021, 9:06 PM
    To your if/else getting bigger and bigger, what I like to do in those cases is to better divide the problem. You could have a mapping between block types and "serializers", that would know how to transform each block:
    Copy code
    $blockSerializers = [
        'image' => ImageSerializer::class,
        'galeriaImgTexto' => GaleriaImgTextoSerializer::class,
        
        ...
    ];
    
    if ($serializer = $blockSerializers[$block->type] ?? null) {
        return (new $serializer)->serialize($block);
    }
    
    return null;
    I don't know... this is just a quick "sketch" 🙂
  • i

    ifox

    09/10/2021, 9:09 PM
    hey guys, one suggestion I have would be to refactor to have a function or class per block type, which you can then compose to reuse what they have in common. a very simple approach in a single file would be:
    Copy code
    php
    public function getContentsAttribute()
    {
      return $this->blocks->where('parent_id', null)->map(function ($block) {
        $adapterMethodName = Str::camel($block->type) . 'ToApi';
        return $this->$adapterMethodName($block);
      });
    }
    
    // let's say you have a block named "quote"
    private function quoteToApi($block)
    {
        return [
            'text' => $block->translatedInput('quote'),
            'author' => $block->input('author'),
        ];
    }
    
    // and so on for your other blocks
  • i

    ifox

    09/10/2021, 9:11 PM
    @User nice haha, you did the class one and I did the function one. hope that helps @User
  • u

    user

    09/10/2021, 9:13 PM
    Nice guys
  • u

    user

    09/10/2021, 9:13 PM
    Thanks
  • u

    user

    09/10/2021, 9:14 PM
    The @User seems to be easier hahaha
  • u

    user

    09/10/2021, 9:15 PM
    It will help
  • u

    user

    09/10/2021, 9:15 PM
    Thanks again
  • a

    agnonym

    09/11/2021, 8:54 AM
    Hi all, I started using JSON columns and making my own functions until I saw there already is a dedicated Trait (not documented, but existing since 2019 😆 https://github.com/area17/twill/pull/410). Problem is the Trait seems to only work on main Model attributes and not translated ones, and it doesn't work if you use the Laravel cast mechanism (https://laravel.com/docs/8.x/eloquent-mutators#array-and-json-casting) as the Trait uses json_decode on the object attribute (and it's already decoded). Have I well understood how the Trait is supposed to work and its limitations or did I miss something? Thanks for you help
  • p

    pboivin

    09/11/2021, 3:22 PM
    Hi @User, I agree that this should be in the docs. There are quite a few nuggets like this in Twill internals that have yet to be fully documented! I haven't used this feature much but I did a quick test and I can confirm that
    HandleFieldsGroups
    is not quite optimized for translations.
  • p

    pboivin

    09/11/2021, 3:23 PM
    If I may suggest a quick workaround, you could introduce a helper method for your field group: In your model:
    Copy code
    php
        protected $fillable = [
            // ...
            
            'header',
        ];
    
        protected $casts = [
            'header' => 'array',
        ];
        
        // ...
    
        public function headerField($name)
        {
            if ($value = $this->header[$name] ?? null) {
                $value = $value[app()->getLocale()];
            }
            return $value;
        }
    In your views:
    Copy code
    {{ $item->headerField('header_title') }}
    ... something like that. This could be improved 🙂
  • a

    agnonym

    09/11/2021, 3:39 PM
    Thanks for your reply on a saturday ☺️ The problem is not in the view for the moment 🙂 It's for editing the fields of the group in the admin form and have them stored. The
    HandleTranslations
    Trait needs to have this structure:
    Copy code
    "meta_title" => [
        "fr" => null
        "en" => null
      ]
    to map it into this structure:
    Copy code
    "fr" => [
        "meta_title" => null
      ]
      "en" => [
        "meta_title" => null
      ]
    I'll have to make more tests and see how to handle this! But does it work for you to define the attribute cast to array and having no error in admin edition with the
    HandleFieldsGroups
    Trait?
  • p

    pboivin

    09/11/2021, 3:42 PM
    Shoot, you're right. I went too fast 🙂
  • p

    pboivin

    09/11/2021, 3:43 PM
    So yeah, the problem I see when I refresh the form is with
    getFormFieldsHandleFieldsGroups()
    , as you noted.
1...919293...484Latest