You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Nova Inline Relationship is meant to present a relationship based property as an inline property for a Laravel Nova Resource. This project is under active development, and currently only supports singular relationships. You are welcome to request or contribute by opening an issue.
4
+
Nova Inline Relationship is meant to present a relationship based property as an inline property for a Laravel Nova Resource. You are welcome to request or contribute by opening an issue.
After installation, your model should include the `KirschbaumDevelopment\NovaInlineRelationship\Traits\HasRelatedAttributes` trait and you must implement the `KirschbaumDevelopment\NovaInlineRelationship\Contracts\MappableRelationships` Contract.
22
23
23
-
You must also define a static `getPropertyMap` function in the model which should return the required mapping between local and related attribute.
24
-
25
-
**_NOTE:_** You must add relationships in `relationship.attribute` format in the map. Nested relationships are currently not supported.
24
+
You must also define a static `getPropertyMap` function in the model which should return the required list of properties from your related models you want to show inline.
25
+
In this example `Employee` model has two related models `EmployeeProfile` and `EmployeeBill`
26
26
27
27
```php
28
28
use KirschbaumDevelopment\NovaInlineRelationship\Traits\HasRelatedAttributes;
@@ -39,6 +39,15 @@ class Employee extends Model implements MappableRelationships
39
39
{
40
40
return $this->hasOne(EmployeeProfile::class);
41
41
}
42
+
43
+
/**
44
+
* @return HasMany
45
+
*/
46
+
public function bills(): HasMany
47
+
{
48
+
return $this->hasMany(EmployeeBill::class);
49
+
}
50
+
42
51
43
52
/**
44
53
* Should return property map as key value pair.
@@ -47,7 +56,64 @@ class Employee extends Model implements MappableRelationships
@@ -56,9 +122,7 @@ class Employee extends Model implements MappableRelationships
56
122
57
123
## Usage
58
124
59
-
Once you add this relationship map you can use the keys for this relationship map as a normal attribute inside your model's nova resource.
60
-
61
-
**_NOTE:_** These fields are in essence [Computed Fields](https://nova.laravel.com/docs/2.0/resources/fields.html#computed-fields), and are subjected to the same limitations. Since they are not associated with a database column, these fields will not be `sortable`.
125
+
Once you add this relationship map you can add `NovaInlineRelationship` to your Model's resource with a relationship.
62
126
63
127
```php
64
128
namespace App\Nova;
@@ -72,13 +136,150 @@ class Employee extends Resource
72
136
return [
73
137
//...
74
138
75
-
Text::make('Phone #', 'phone')
76
-
->rules('required'),
139
+
NovaInlineRelationship::make('Profile'),
140
+
141
+
NovaInlineRelationship::make('Bills'),
142
+
];
143
+
}
144
+
}
145
+
```
146
+
**_NOTE:_** These fields are in essence [Computed Fields](https://nova.laravel.com/docs/2.0/resources/fields.html#computed-fields), and are subjected to the same limitations. Since they are not associated with a database column, these fields will not be `sortable`.
After setup you can add new related models directly while creating a new base model. For example, If you have added an `EmployeeProfile` model as a related model for your `Employee` model, you can infact add information for a related `EmployeeProfile` model without going through an additional step. You can use the `Add new Profile` button to add a new blank profile
153
+
154
+

155
+
156
+
## Adding multiple related models
157
+
158
+
If your relationship is a `one-to-many` relationship you can add multiple related models in a one go.
159
+
160
+

161
+
162
+
## Viewing related models
163
+
164
+
Once you add your related models and visit your base model's detail view you can watch your related models in a collapsible view. So when you will watch an `Employee` model, you can watch `EmployeeProfile` models in a collapsible view.
You can specify custom error messages for field in format specified in the [laravel documentation](https://laravel.com/docs/5.8/validation#customizing-the-error-messages).
191
+
192
+
For every field you can specify validation messages for each rule. You can use attribute wildcards like `:attribute` in your error messages as well.
193
+
194
+
```php
195
+
use KirschbaumDevelopment\NovaInlineRelationship\Traits\HasRelatedAttributes;
196
+
use KirschbaumDevelopment\NovaInlineRelationship\Contracts\MappableRelationships;
197
+
198
+
class Employee extends Model implements MappableRelationships
199
+
{
200
+
// ...
201
+
202
+
/**
203
+
* Should return property map as key value pair.
204
+
*
205
+
* @return array
206
+
*/
207
+
public static function getPropertyMap(): array
208
+
{
209
+
return [
210
+
'profile' => [
211
+
'phone' => [
212
+
'component => Number::class,
213
+
'label' => 'Phone',
214
+
'rules' => 'required|numeric',
215
+
'placeholder' => 'Add Phone',
216
+
'messages' => [
217
+
'required' => 'You must add a :attribute for this profile.',
218
+
'numeric' => 'Your :attribute must be numeric'
219
+
],
220
+
],
221
+
],
79
222
];
80
223
}
224
+
225
+
// ...
81
226
}
227
+
```
228
+
229
+
## Settings
230
+
231
+
You can pass on following items for your related model's attributes:-
232
+
1.`component`: The component use to render the field. You should provide the component name specified in the field like Text::class, Number::class, Boolean::class and so on.
233
+
2.`label`: Label for your field. This will also be used as the field name in error messages.
234
+
3.`rules`: A rule string in [Laravel Validation format](https://laravel.com/docs/5.8/validation#available-validation-rules).
235
+
4.`messages`: An array of error messages to be used in validation.
236
+
5.`placeholder`: A placeholder for your field.
237
+
6.`options`: Additional options for your components
238
+
239
+
## Supported fields
240
+
241
+
You can use any field you can add to your Nova resource with `Field::make` syntax. The following native Nova 2.0 fields are confirmed to work.
242
+
243
+
- Boolean Field
244
+
- Code Field
245
+
- Country Field
246
+
- Currency Field
247
+
- Date Field
248
+
- DateTime Field
249
+
- Markdown Field
250
+
- Number Field
251
+
- Password Field
252
+
- Place Field
253
+
- Select Field
254
+
- Text Field
255
+
- Textarea Field
256
+
- Timezone Field
257
+
- Trix Field
258
+
- Avatar Field
259
+
- Image Field
260
+
- File Field
261
+
262
+
### Select Component
263
+
To pass dropdown options to your select component, pass an array of key values to options
264
+
```php
265
+
'component => Select::class,
266
+
'options' => [
267
+
'options' => [
268
+
['label' => 'Yes', 'value' => true],
269
+
['label' => 'No', 'value' => false],
270
+
],
271
+
]
272
+
```
273
+
274
+
### Number/Currency Component
275
+
You can pass `min`, `max` and `step` to number and currency field
0 commit comments