Skip to content

Commit

Permalink
Create polymorphcis
Browse files Browse the repository at this point in the history
  • Loading branch information
zareismail committed Feb 15, 2020
1 parent cf94b16 commit 6a2b6d1
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 68 deletions.
61 changes: 60 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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(),
];
}
```
74 changes: 74 additions & 0 deletions src/MorphToMany.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Armincms\Fields;

use Laravel\Nova\Fields\Select;

class MorphToMany 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;
}
}
69 changes: 2 additions & 67 deletions src/MorphedByMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
}

0 comments on commit 6a2b6d1

Please sign in to comment.