From 6a2b6d1795390db08ed8afcf535e017535c8fcdc Mon Sep 17 00:00:00 2001 From: esmaielzareh Date: Sat, 15 Feb 2020 15:34:36 +0330 Subject: [PATCH] Create polymorphcis --- README.md | 61 ++++++++++++++++++++++++++++++++++- src/MorphToMany.php | 74 +++++++++++++++++++++++++++++++++++++++++++ src/MorphedByMany.php | 69 ++-------------------------------------- 3 files changed, 136 insertions(+), 68 deletions(-) create mode 100644 src/MorphToMany.php diff --git a/README.md b/README.md index 92a7b41..ca5738a 100644 --- a/README.md +++ b/README.md @@ -113,4 +113,63 @@ You can use the `duplicate` feature for repetitively attach a resource to anothe ``` ## Polymorphic Relation -Using for the polymorphic relationships is like non-polymorphic. \ No newline at end of file +Using for the polymorphic relationships is like non-polymorphic. follow the example: + + +``` + use Armincms\Fields\MorphToMany; + + + + /** + * Get the fields displayed by the resource. + * + * @param \Illuminate\Http\Request $request + * @return array + */ + public function fields(Request $request) + { + return [ + MorphToMany::make(__("Label"), 'relationName', RelatedResource::class) + ->fields(function() { + return [ + Text::make('Price') + ->rules('required', 'numeric'), + ]; + }) + ->duplicate() + ->pivots(), + ]; + } + +``` + +or + +``` + use Armincms\Fields\MorphedByMany; + + + + /** + * Get the fields displayed by the resource. + * + * @param \Illuminate\Http\Request $request + * @return array + */ + public function fields(Request $request) + { + return [ + MorphedByMany::make(__("Label"), 'relationName', RelatedResource::class) + ->fields(function() { + return [ + Text::make('Price') + ->rules('required', 'numeric'), + ]; + }) + ->duplicate() + ->pivots(), + ]; + } + +``` \ No newline at end of file diff --git a/src/MorphToMany.php b/src/MorphToMany.php new file mode 100644 index 0000000..48d919b --- /dev/null +++ b/src/MorphToMany.php @@ -0,0 +1,74 @@ +pivots(); + $this->fields(function() {}); + } + + /** + * Specify the callback to be executed to retrieve the pivot fields. + * + * @param callable $callback + * @return $this + */ + public function fields(callable $callback) + { + $this->fieldsCallback = function($request, $resource) use ($callback) { + $relation = $this->getMorphToMany($resource); + + return collect(call_user_func($callback, $request, $resource))->prepend( + Select::make(__("Resource"), $relation->getMorphType($resource)) + ->options([ + $relation->getMorphClass() => $resource::label(), + ]) + ->onlyOnForms() + ->required() + ->rules('required') + ->withMeta(['value' => $relation->getMorphClass()]) + )->all(); + }; + + return $this; + } + + /** + * Indicated the MorphToMany relationship. + * + * @return \Illuminate\Database\Eloquent\Relations\MorphToMany + */ + protected function getMorphToMany($resource) + { + $model = $resource::newModel(); + + return $model->{$this->manyToManyRelationship}(); + } + + /** + * Set the pivots attachment status. + * + * @return string + */ + public function pivots(bool $pivots = true) + { + $this->pivots = true; + + return $this; + } +} diff --git a/src/MorphedByMany.php b/src/MorphedByMany.php index 6646c21..8b2413c 100644 --- a/src/MorphedByMany.php +++ b/src/MorphedByMany.php @@ -4,71 +4,6 @@ use Laravel\Nova\Fields\Select; -class MorphedByMany extends BelongsToMany -{ - /** - * Create a new field. - * - * @param string $name - * @param string|null $attribute - * @param string|null $resource - * @return void - */ - public function __construct($name, $attribute = null, $resource = null) - { - parent::__construct($name, $attribute, $resource); - - $this->pivots(); - $this->fields(function() {}); - } - - /** - * Specify the callback to be executed to retrieve the pivot fields. - * - * @param callable $callback - * @return $this - */ - public function fields(callable $callback) - { - $this->fieldsCallback = function($request, $resource) use ($callback) { - $relation = $this->getMorphToMany($resource); - - return collect(call_user_func($callback, $request, $resource))->prepend( - Select::make(__("Resource"), $relation->getMorphType($resource)) - ->options([ - $relation->getMorphClass() => $resource::label(), - ]) - ->onlyOnForms() - ->required() - ->rules('required') - ->withMeta(['value' => $relation->getMorphClass()]) - )->all(); - }; - - return $this; - } - - /** - * Indicated the MorphToMany relationship. - * - * @return \Illuminate\Database\Eloquent\Relations\MorphToMany - */ - protected function getMorphToMany($resource) - { - $model = $resource::newModel(); - - return $model->{$this->manyToManyRelationship}(); - } - - /** - * Set the pivots attachment status. - * - * @return string - */ - public function pivots(bool $pivots = true) - { - $this->pivots = true; - - return $this; - } +class MorphedByMany extends MorphToMany +{ }