Skip to content

Commit

Permalink
Merge branch 'timcv-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjacho committed Oct 18, 2019
2 parents f0b9efd + dd6948f commit 2557cf6
Show file tree
Hide file tree
Showing 8 changed files with 11,367 additions and 16 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,22 @@ composer require benjacho/belongs-to-many-field
## Deprecation
Method relationModel() no more needed, to prevent conflicts it will be there. And trait HasBelongsToMany no more neede too, both will be in repo, but doesn't work.

Method options is not needed anymore.

### Usage

To use in nova 1.0 use 0.3 in nova 2.0 use 0.4 and above.

In the resource you need to pass:

- Method make (label, many to many relationship, Nova Resource Relationship)
- Method options (Here you pass options that you need to render in Multiple Select, you can pass Querys, use get() method for that purpose)
- You dont need to pass onlyOnForms(), it is by default.
- It is available in index, detail and forms!

```php
use Benjacho\BelongsToManyField\BelongsToManyField;

public function fields(Request $request){
BelongsToManyField::make('Role Label', 'roles', 'App\Nova\Role')->options(\App\Role::all()),
BelongsToManyField::make('Role Label', 'roles', 'App\Nova\Role'),
}
```

Expand All @@ -40,7 +41,7 @@ Optional
use Benjacho\BelongsToManyField\BelongsToManyField;

public function fields(Request $request){
BelongsToManyField::make('Role Label', 'roles', 'App\Nova\Role')->options(\App\Role::all())->optionsLabel('title'),
BelongsToManyField::make('Role Label', 'roles', 'App\Nova\Role')->optionsLabel('title'),
}
```

Expand All @@ -50,7 +51,7 @@ public function fields(Request $request){
use Benjacho\BelongsToManyField\BelongsToManyField;

public function fields(Request $request){
BelongsToManyField::make('Role Label', 'roles', 'App\Nova\Role')->options(\App\Role::all())->isAction(),
BelongsToManyField::make('Role Label', 'roles', 'App\Nova\Role')->isAction(),
}
```
To obtain the data that is send in action do it:
Expand Down Expand Up @@ -86,7 +87,7 @@ This package implement all Laravel Validations, you need to pass the rules in ru
use Benjacho\BelongsToManyField\BelongsToManyField;

public function fields(Request $request){
BelongsToManyField::make('Role Label', 'roles', 'App\Nova\Role')->options(\App\Role::all())->relationModel(\App\User::class)->rules('required', 'min:1', 'max:5', 'size:3' new CustomRule),
BelongsToManyField::make('Role Label', 'roles', 'App\Nova\Role')->relationModel(\App\User::class)->rules('required', 'min:1', 'max:5', 'size:3' new CustomRule),
}
```

Expand Down
11,277 changes: 11,276 additions & 1 deletion dist/js/field.js

Large diffs are not rendered by default.

15 changes: 13 additions & 2 deletions resources/js/components/DetailField.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
<template>
<panel-item :field="field" />
<panel-item :field="field">
<div slot="value">
<span v-for="(resource, key) in field.value">
<a
:href="$router.options.base + '/resources/' + field.attribute +'/' + resource.id"
class="no-underline dim text-primary font-bold"
>
{{resource.name}}<span v-if="key < (field.value.length - 1)">,</span>
</a>
</span>
</div>
</panel-item>
</template>

<script>
export default {
props: ['resource', 'resourceName', 'resourceId', 'field'],
props: ['resource', 'resourceName', 'resourceId', 'field', 'abc'],
}
</script>
28 changes: 24 additions & 4 deletions resources/js/components/FormField.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<template>
<default-field :field="field" :errors="errors">
<template slot="field">
<div :style="{height: field.height ? field.height : 'auto'}">
<div :style="{height: field.height ? field.height : 'auto'}" class="relative">
<div v-if="loading" class="py-6 px-8 flex justify-center items-center absolute pin z-50 bg-white">
<loader class="text-60"/>
</div>
<multi-select
:options="options"
v-bind="multiSelectProps"
Expand All @@ -28,9 +31,10 @@ export default {
return {
options: [],
optionsLabel: "name",
loading: true,
}
},
computed: {
multiSelectProps() {
return {
Expand All @@ -49,11 +53,27 @@ export default {
* Set the initial, internal value for the field.
*/
setInitialValue() {
this.options = this.field.options;
this.optionsLabel = this.field.optionsLabel ? this.field.optionsLabel : 'name';
this.value = this.field.value || ''
this.fetchOptions();
},
fetchOptions () {
if (this.field.options) {
this.options = this.field.options
this.loading = false;
return;
}
let baseUrl = '/nova-vendor/belongs-to-many-field/'
Nova.request(baseUrl + this.resourceName + '/' + 'options/' + this.field.attribute)
.then((data) => {
this.options = data.data;
this.loading = false;
})
},
/**
* Fill the given FormData object with the field's internal value.
*/
Expand All @@ -71,4 +91,4 @@ export default {
}
</script>

<style src="vue-multiselect/dist/vue-multiselect.min.css"></style>
<style src="vue-multiselect/dist/vue-multiselect.min.css"></style>
12 changes: 11 additions & 1 deletion resources/js/components/IndexField.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
<template>
<span>{{ field.value }}</span>
<div>
<span v-for="(resource, key) in field.value">
<a
:href="field.attribute +'/' + resource.id"
class="no-underline dim text-primary font-bold"
>
{{resource['name']}}<span v-if="key < (field.value.length - 1)">,</span>
</a>

</span>
</div>
</template>

<script>
Expand Down
28 changes: 28 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Laravel\Nova\Http\Requests\NovaRequest;

Route::get('/{resource}/options/{relationship}', function(NovaRequest $request, $parent, $relationship) {
$resourceClass = $request->newResource();

$field = $resourceClass
->availableFields($request)
->where('component', 'BelongsToManyField')
->where('attribute', $relationship)
->first();

$query = $field->resourceClass::newModel();

return $field->resourceClass::relatableQuery($request, $query)->get()
->mapInto($field->resourceClass)
->filter(function ($resource) use ($request, $field) {
return $request->newResource()->authorizedToAttach($request, $resource->resource);
})->map(function($resource) {
return [
'id' => $resource->id,
'name' => $resource->title(),
'value' => $resource->getKey(),
];
})->sortBy('name')
->values();
});
4 changes: 2 additions & 2 deletions src/BelongsToManyField.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

class BelongsToManyField extends Field
{
public $showOnIndex = false;
public $showOnDetail = false;
public $showOnIndex = true;
public $showOnDetail = true;
public $isAction = false;
public $height = '350px';
/**
Expand Down
6 changes: 6 additions & 0 deletions src/FieldServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ public function boot()
Nova::script('BelongsToManyField', __DIR__.'/../dist/js/field.js');
Nova::style('BelongsToManyField', __DIR__.'/../dist/css/field.css');
});

$this->app->booted(function () {
\Route::middleware(['nova'])
->prefix('nova-vendor/belongs-to-many-field')
->group(__DIR__.'/../routes/api.php');
});
}

/**
Expand Down

0 comments on commit 2557cf6

Please sign in to comment.