Skip to content

Commit

Permalink
Fix error when editing related resource (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
milewski authored Sep 30, 2023
1 parent 799409b commit f4cc845
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 244 deletions.
3 changes: 1 addition & 2 deletions dist/js/field.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"scripts": {
"watch": "mix watch",
"production": "mix --production",
"nova:install": "npm --prefix='../vendor/laravel/nova' ci"
"nova:install": "npm --prefix='../../vendor/laravel/nova' ci"
},
"devDependencies": {
"@vue/compiler-sfc": "^3.3.4",
Expand Down
94 changes: 0 additions & 94 deletions resources/js/components/CustomIndexComponent.vue

This file was deleted.

16 changes: 9 additions & 7 deletions resources/js/components/CustomRelationshipField.vue
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<template>

<IndexComponent
<ResourceIndex
:field="field"
:resource-name="field.resourceName"
:via-resource="resourceName"
:via-resource-id="resourceId"
:via-relationship="`CustomRelationshipField:${ field.attribute }`"
:relationship-type="'hasMany'"
:via-relationship="null"
:relationship-type="`CustomRelationshipField:${ encodedAttribute }`"
@actionExecuted="actionExecuted"
:load-cards="false"
:initialPerPage="field.perPage || 5"
:initial-per-page="field.perPage || 5"
:should-override-meta="false"
:custom-relationship-field-attribute="field.attribute"
:custom-relationship-field-label="field.name"
Expand All @@ -19,17 +19,19 @@

<script>
import IndexComponent from './CustomIndexComponent'
export default {
components: { IndexComponent },
emits: [ 'actionExecuted' ],
props: [ 'resourceName', 'resourceId', 'resource', 'field' ],
methods: {
actionExecuted() {
this.$emit('actionExecuted')
},
},
computed: {
encodedAttribute() {
return btoa(`${ this.field.attribute }|_::_|${ this.field.name }`)
},
}
}
</script>
24 changes: 24 additions & 0 deletions src/CustomRelationshipFieldServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
namespace DigitalCreative\CustomRelationshipField;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
use Laravel\Nova\Events\ServingNova;
use Laravel\Nova\Http\Requests\NovaRequest;
use Laravel\Nova\Nova;

class CustomRelationshipFieldServiceProvider extends ServiceProvider
Expand All @@ -15,5 +17,27 @@ public function boot(): void
Nova::serving(static function (ServingNova $event): void {
Nova::script('custom-relationship-field', __DIR__ . '/../dist/js/field.js');
});

$this->app->afterResolving(NovaRequest::class, function (NovaRequest $request): void {

if ($relationshipType = $request->relationshipType) {

if (Str::startsWith($relationshipType, 'CustomRelationshipField')) {

$encoded = Str::after($relationshipType, ':');

[ $attribute, $label ] = Str::of(base64_decode($encoded))->explode('|_::_|');

$request->merge([
'relationshipType' => 'hasMany',
'customRelationshipFieldAttribute' => $attribute,
'customRelationshipFieldLabel' => $label,
]);

}

}

});
}
}
Loading

0 comments on commit f4cc845

Please sign in to comment.