Skip to content

Commit

Permalink
Update browser with pivot table documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
ifox committed Jan 18, 2024
1 parent a93fbbd commit ec97fc0
Showing 1 changed file with 45 additions and 45 deletions.
90 changes: 45 additions & 45 deletions docs/content/1_docs/4_form-fields/10_browser.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,51 +71,7 @@ titled [Adding browser fields to a block](../5_block-editor/04_adding-browser-fi
explanation.

Outside the block editor, browser fields are used to save `belongsToMany` relationships. The relationships can be stored
in Twill's own `related` table or in a custom pivot table.

## Using a custom pivot table

- Create your pivot table:

```php
Schema::create('artists_artworks', function (Blueprint $table) {

createDefaultRelationshipTableFields($table, 'artist', 'artwork');

$table->integer('position')->unsigned()->nullable();

});
```
- Add your `afterSave` actions to your repository (i.e. `ArtistRepository`):

```php
public function afterSave(TwillModelContract $object, array $fields): void
{
$this->updateBrowser($object, $fields, 'artworks');
parent::afterSave($object, $fields);
}
```

- Use getFormFields to hydrate your browser field to your repository:

```php
public function getFormFields(TwillModelContract $object): array
{
$fields = parent::getFormFields($object);

$fields['browsers']['artworks'] = $this->getFormFieldsForBrowser(
$object,
'artworks', //relation
null, //routePrefix
'title', //title
'artworks' //module name
);

return $fields;
}
```

You will NOT need to use the `HasRelated` trait with this set up.
in Twill's own `twill_related` table or in a custom pivot table.

## Using browser fields as related items

Expand Down Expand Up @@ -310,6 +266,50 @@ public function getBrowserData($prependScope = [])
In the presented example, this will make sure only variants of the selected product in the first browser can be selected
in the second one.

## Using a custom pivot table

Using the `HasRelated` trait means that Twill is storing the relationship in a polymorphic table, which can make efficient database queries harder to implement depending on the needs of your frontend. Using a pivot table between 2 Twill models is available if you need it.

- Create your pivot table:

```php
Schema::create('artist_artwork', function (Blueprint $table) {
createDefaultRelationshipTableFields($table, 'artist', 'artwork');
$table->integer('position')->unsigned()->nullable();
});
```

- Add the relationship to your model (i.e. `Artist`):
```php
public function artworks(): BelongsToMany
{
return $this->belongsToMany('artworks')->orderByPivot('position');
}
```

- Configure the `$browsers` property in your repository (i.e. `ArtistRepository`):

```php
protected $browsers = ['artworks'];
```

Additional parameters can also be overridden with an array. When only the browser name is given, the rest of the parameters are inferred from the name.


```php
protected $browsers = [
'artworks' => [
'titleKey' => 'title',
'relation' => 'artworks',
'browserName' => 'artworks',
'moduleName' => 'artworks',
'positionAttribute' => 'position',
],
];
```

For even more control, you can use [updateBrowser()](https://twillcms.com/docs/api/3.x/A17/Twill/Repositories/Behaviors/HandleBrowsers.html#method_updateBrowser) in your own `afterSave()` method and [getFormFieldsForBrowser()](https://twillcms.com/docs/api/3.x/A17/Twill/Repositories/Behaviors/HandleBrowsers.html#method_getFormFieldsForBrowser) in your own `getFormFields()` method.

## Morphable browser fields

While a bit more complex to setup, you can target a morphTo.
Expand Down

0 comments on commit ec97fc0

Please sign in to comment.