FilamentF
Filament2y ago
ouch

Set relationship via $set

I have create form, where I can input catalog number (gets an item) and some fields are autofilled, but now i struggle with loading existing images that are associated with main item. And i want them to load if the existing catalog is inputed.
Items --< Sizes

So i was thinking that i could set relationship item, and somehow load images.
But i couldnt make it to set the relationship.
TextInput::make('catalog')
    ->label(__('filament.warehouse.product.catalog'))
    ->minLength(3)
    ->live()
    ->required()
    ->debounce(500)
    ->afterStateUpdated(function (Get $get, Set $set, $state) {
        if (!empty($state)){
            $query = Item::where('catalog', $state);
            if ($query->exists()){
                $item = $query->get()->first();
                //setting relation?             
            }
        }
    })
    ->datalist(fn() =>  Item::distinct('catalog')->pluck('catalog')->toArray()),

Group::make()
  ->relationship('item')
  ->schema([
      Forms\Components\SpatieMediaLibraryFileUpload::make('images')
          ->imagePreviewHeight('125')
          ->reactive()
          ->image()
          ->multiple()
          ->optimize('webp')
          ->resize(30)
          ->conversion('img')
          ->label('')
          ->collection(Item::ITEM_COLLECTION_NAME)
          ->directory('products')
          ->imageEditor()
          ->enableReordering(),
])
Was this page helpful?