Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HasMany relation and media library inside the relation #531

Open
Skiexx opened this issue Dec 20, 2024 · 0 comments
Open

HasMany relation and media library inside the relation #531

Skiexx opened this issue Dec 20, 2024 · 0 comments

Comments

@Skiexx
Copy link

Skiexx commented Dec 20, 2024

When trying to create a HasMany link using Resolver and custom Layout, there is a problem inside the object.

Using trait HasMediaLibrary there is a problem that the library considers the parent model as containing HasMedia interface. The problem is with HasMediaLibrary and the method getUnderlyingMediaModel, which returns not the child model, but the parent model as the owner of HasMedia. As a result it returns the error 'Origin HasMedia model not found.'

Code:

class ChildrenLayout extends Layout implements HasMedia
{
    use HasMediaLibrary;
    ...
    public function fields()
    {
        return [
            ID::make()->sortable(),

            Text::make(__('Title'), 'title')
                ->rules('required', 'max:50')
                ->filterable()
                ->sortable(),

            Textarea::make(__('Text'), 'text')
                ->rules('required', 'max:250')
                ->hideFromIndex(),

            Images::make(__('Image'), 'image')
                ->required()
                ->rules('required')
                ->singleImageRules('required'),
        ];
    }
}


class ChildrenResolver implements ResolverInterface
{
    public function get($resource, $attribute, $layouts)
    {
        $childrens = $resource->children()->orderBy('order')->get();
        $layout = $layouts->find('children');

        return $childrens->map(function (Children $children) use ($layout) {
            return $layout->duplicateAndHydrate(
                $children->id,
                [
                    'id' => $children->id,
                    'title' => $children->title,
                    'text' => $children->text,
                    'order' => $children->order,
                    'image' => $children->getMedia('image')
                ]);
        })->filter()->values();
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant