Skip to content

Commit 12dfcec

Browse files
authored
Merge pull request #7 from kirschbaum-development/feature/add-macro
Use inline macro for loading NovaInlineRelationship
2 parents 5dbd376 + 3f809fe commit 12dfcec

6 files changed

Lines changed: 190 additions & 454 deletions

File tree

README.md

Lines changed: 7 additions & 190 deletions
Original file line numberDiff line numberDiff line change
@@ -17,113 +17,12 @@ You can install this package in a Laravel app that uses [Nova](https://nova.lara
1717
composer require kirschbaum-development/nova-inline-relationship
1818
```
1919

20-
## Setup
21-
22-
After installation, your model should include the `KirschbaumDevelopment\NovaInlineRelationship\Traits\HasRelatedAttributes` trait and you must implement the `KirschbaumDevelopment\NovaInlineRelationship\Contracts\MappableRelationships` Contract.
23-
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-
27-
```php
28-
use KirschbaumDevelopment\NovaInlineRelationship\Traits\HasRelatedAttributes;
29-
use KirschbaumDevelopment\NovaInlineRelationship\Contracts\MappableRelationships;
30-
31-
class Employee extends Model implements MappableRelationships
32-
{
33-
use HasRelatedAttributes;
34-
35-
/**
36-
* @return HasOne
37-
*/
38-
public function profile(): HasOne
39-
{
40-
return $this->hasOne(EmployeeProfile::class);
41-
}
42-
43-
/**
44-
* @return HasMany
45-
*/
46-
public function bills(): HasMany
47-
{
48-
return $this->hasMany(EmployeeBill::class);
49-
}
50-
51-
52-
/**
53-
* Should return property map as key value pair.
54-
*
55-
* @return array
56-
*/
57-
public static function getPropertyMap(): array
58-
{
59-
return [
60-
'profile' => [
61-
'nickname' => [
62-
'label' => 'nickname',
63-
'component' => \Laravel\Nova\Fields\Text::class,
64-
'rules' => 'required',
65-
'placeholder' => 'Add Nickname',
66-
'messages' => ['required' => 'You must add a :attribute for this profile.'],
67-
],
68-
'phone' => [
69-
'component' => \Laravel\Nova\Fields\Number::class,
70-
'label' => 'Phone',
71-
'rules' => 'required|numeric',
72-
'placeholder' => 'Add Phone',
73-
],
74-
'photo' => [
75-
'component' => \Laravel\Nova\Fields\Image::class,
76-
],
77-
'snippet' => [
78-
'component' => \Laravel\Nova\Fields\Code::class,
79-
],
80-
'country' => [
81-
'component' => \Laravel\Nova\Fields\Country::class,
82-
],
83-
'settings' => [
84-
'component' => \Laravel\Nova\Fields\KeyValue::class,
85-
],
86-
'dob' => [
87-
'component' => \Laravel\Nova\Fields\Date::class,
88-
],
89-
],
90-
'bills' => [
91-
'pending' => [
92-
'component' => \Laravel\Nova\Fields\Boolean::class,
93-
'label' => 'Pending',
94-
],
95-
'description' => [
96-
'component' => \Laravel\Nova\Fields\Trix::class,
97-
'label' => 'Description',
98-
'placeholder' => 'Add your description here',
99-
],
100-
'address' => [
101-
'component' => \Laravel\Nova\Fields\Place::class,
102-
],
103-
'amount' => [
104-
'component' => \Laravel\Nova\Fields\Currency::class,
105-
],
106-
'submitted_at' => [
107-
'component' => \Laravel\Nova\Fields\DateTime::class,
108-
],
109-
'auth_code' => [
110-
'component' => \Laravel\Nova\Fields\Password::class,
111-
],
112-
'notes' => [
113-
'component' => \Laravel\Nova\Fields\Markdown::class,
114-
],
115-
],
116-
];
117-
}
118-
119-
// ...
120-
}
121-
````
122-
12320
## Usage
12421

125-
Once you add this relationship map you can add `NovaInlineRelationship` to your Model's resource with a relationship.
22+
To use `NovaInlineRelationship` to your Model's resource all you need to do is to add an inline method to the regular syntax of your related Model's Resource.
12623

24+
If we assume that an Employee Models has a one-to-one relationship with `EmployeeProfile` and one-to-many relationship with `EmployeeBill` model then the code will look like:
25+
12726
```php
12827
namespace App\Nova;
12928

@@ -136,14 +35,14 @@ class Employee extends Resource
13635
return [
13736
//...
13837

139-
NovaInlineRelationship::make('Profile'),
38+
HasOne::make('Profile', 'profile', EmployeeProfile::class)->inline(),
14039

141-
NovaInlineRelationship::make('Bills'),
40+
HasMany::make('Bills', 'bills', EmployeeBill::class)->inline(),
14241
];
14342
}
14443
}
14544
```
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`.
45+
**_NOTE:_** You will need to add NovaResources for `EmployeeProfile` and `EmployeeBill` Model and all the field and rules will be fetch from it.
14746

14847
## Adding related models
14948

@@ -177,65 +76,6 @@ For `one-to-many` relationships you can drag and drop related models to rearrang
17776

17877
You can delete related models from the base model's update view by using the `delete button` at the top right corner.
17978

180-
## Validating related models
181-
182-
You can specify the validation rule for the fields in your related model using the `getPropertyMap()` function.
183-
184-
An error will be displayed next to field if a validation rule is not met.
185-
186-
![Validating Models](screenshots/Validation.png "Validating Models")
187-
188-
### Specifying custom error messages
189-
190-
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-
],
222-
];
223-
}
224-
225-
// ...
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-
23979
## Supported fields
24080

24181
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.
@@ -259,29 +99,6 @@ You can use any field you can add to your Nova resource with `Field::make` synta
25999
- Image Field
260100
- File Field
261101

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
276-
```php
277-
'component' => Currency::class,
278-
'options' => [
279-
'min' => 0,
280-
'max' => 100,
281-
'step' => 10,
282-
],
283-
```
284-
285102
## Changelog
286103

287104
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
@@ -292,7 +109,7 @@ Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
292109

293110
## Security
294111

295-
If you discover any security related issues, please email brandon@kirschbaumdevelopment.com or nathan@kirschbaumdevelopment.com instead of using the issue tracker.
112+
If you discover any security related issues, please email navneet@kirschbaumdevelopment.com or nathan@kirschbaumdevelopment.com instead of using the issue tracker.
296113

297114
## Sponsorship
298115

src/Contracts/MappableRelationships.php

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)