Skip to content

[FEATURE] Now duplicate field supports nearly all relations #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 51 additions & 14 deletions src/Http/Controllers/DuplicateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@

namespace Jackabox\DuplicateField\Http\Controllers;

use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Str;

class DuplicateController extends Controller
{
Expand All @@ -14,40 +21,70 @@ public function duplicate(Request $request)
{
// Replicate the model
$model = $request->model::where('id', $request->id)->first();

if (!$model) {
return [
'status' => 404,
'message' => 'No model found.',
'destination' => config('nova.url') . config('nova.path') . '/resources/' . $request->resource . '/'
];
}

$newModel = $model->replicate();
$newModel->push();

if (isset($request->relations) && !empty($request->relations)) {
// load the relations
$model->load($request->relations);

foreach ($model->getRelations() as $relation => $items) {
// works for hasMany
foreach ($items as $item) {
// clean up our models, remove the id and remove the appends
unset($item->id);
$item->setAppends([]);

// create a relation on the new model with the data.
$newModel->{$relation}()->create($item->toArray());
$relation_data = $model->{$relation}();
switch(get_class($relation_data)){
case BelongsToMany::class:
case MorphToMany::class:
foreach ($items as $item) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably want to extract these to individual functions to clean up this, when all classes are added it could become hard to read/manage.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you wish. I think you can merge this changes to this currently version of your repository.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems good, I'll give it a test and double-check

$pivot = $item->pivot;
unset($pivot->id);
unset($pivot->created_at);
unset($pivot->updated_at);

$foreignPivotKey = $relation_data->getForeignPivotKeyName();
unset($pivot->{$foreignPivotKey});
$relatedPivotKey = $relation_data->getRelatedPivotKeyName();
unset($pivot->{$relatedPivotKey});

$newModel->{$relation}()->attach($item->id , $pivot->toArray());
}
break;
case HasMany::class:
case HasOne::class:
case MorphMany::class:
case MorphOne::class:
foreach ($items as $item) {
// clean up our models, remove the id and remove the appends
unset($item->id);
$item->setAppends([]);

// create a relation on the new model with the data.
$newModel->{$relation}()->create($item->toArray());
}
break;
default:
return [
'status' => 404,
'message' => 'This relation is not supported',
'destination' => config('nova.url') . config('nova.path') . '/resources/' . $request->resource . '/'
];
break;
}
}
}

// return response and redirect.
return [
'status' => 200,
'message' => 'Done',
'destination' => url(config('nova.path') . '/resources/' . $request->resource . '/' . $newModel->id)
];
}
}
}