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

How to replicate with relationship? #207

Open
bocanhcam opened this issue Feb 13, 2023 · 0 comments
Open

How to replicate with relationship? #207

bocanhcam opened this issue Feb 13, 2023 · 0 comments

Comments

@bocanhcam
Copy link

I have a resource called Product and it has a relationship with another named Section.

Section is multiple select options of Product whenever I create or update Product.

Now, Whenever I replicate Product I want it also to replicate Section (auto select Section like the one replicated).

I have a workaround like this. It seems to work but I'm not so sure.

Does Nova or this package support a way to do it?

Thanks for your help!

public function fields(NovaRequest $request): array
{
   return [
     //..
    $this->sections(),
  ];
}
public function replicate()
{
   $productId = request()->get('fromResourceId');
        $sections = \App\Models\ServiceSection::whereHas('products', function(Builder $q) use ($productId) {
            $q->where('product_id', $productId);
        })->get();

        if (!empty($sections)){
            self::$isReplicate = true;
            $sections->map(function ($section) {
                self::$sectionOptions[] = [$section->id];
            });
        }

    return parent::replicate();
}
/**
     * @throws Exception
     */
    protected function sections(): Multiselect
    {
        $sections = MultiTagField::make(__('Service Sections'), 'sections')
            ->api('/api/sections-by-service', ServiceSection::class)
            ->belongsToMany(ServiceSection::class, false)
            ->hideFromIndex();

        if (self::$isReplicate){
            $sections->withMeta(['value' => self::$sectionOptions]);
        }

        return $sections;
    }
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