Skip to content

Commit cc907c2

Browse files
committed
Merge branch 'v5' into singleton-view-namespaces
2 parents d054260 + ec35cab commit cc907c2

File tree

3 files changed

+51
-10
lines changed

3 files changed

+51
-10
lines changed

src/app/Library/CrudPanel/Traits/Create.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ private function handleManyRelationItemRemoval($modelInstance, $removedEntries,
264264
// developer set force_delete => true, so we don't care if it's nullable or not,
265265
// we just follow developer's will
266266
if ($forceDelete) {
267-
return $removedEntries->delete();
267+
return $removedEntries->lazy()->each->delete();
268268
}
269269

270270
// get the default that could be set at database level.
@@ -273,7 +273,7 @@ private function handleManyRelationItemRemoval($modelInstance, $removedEntries,
273273
// if column is not nullable in database, and there is no column default (null),
274274
// we will delete the entry from the database, otherwise it will throw and ugly DB error.
275275
if (! $relationColumnIsNullable && $dbColumnDefault === null) {
276-
return $removedEntries->delete();
276+
return $removedEntries->lazy()->each->delete();
277277
}
278278

279279
// if column is nullable we just set it to the column default (null when it does exist, or the default value when it does).
@@ -316,10 +316,10 @@ private function createManyEntries($entry, $relation, $relationMethod, $relation
316316
$this->createRelationsForItem($item, $relationInputs);
317317
}
318318

319-
// use the collection of sent ids to match agains database ids, delete the ones not found in the submitted ids.
319+
// use the collection of sent ids to match against database ids, delete the ones not found in the submitted ids.
320320
if (! empty($relatedItemsSent)) {
321321
// we perform the cleanup of removed database items
322-
$entry->{$relationMethod}()->whereNotIn($relatedModelLocalKey, $relatedItemsSent)->delete();
322+
$entry->{$relationMethod}()->whereNotIn($relatedModelLocalKey, $relatedItemsSent)->lazy()->each->delete();
323323
}
324324
}
325325
}

src/app/Library/CrudPanel/Traits/Fields.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -479,19 +479,19 @@ public function getStrippedSaveRequest($request)
479479
{
480480
$setting = $this->getOperationSetting('strippedRequest');
481481

482+
// if a closure was passed
483+
if (is_callable($setting)) {
484+
return $setting($request);
485+
}
486+
482487
// if an invokable class was passed
483488
// eg. \App\Http\Requests\BackpackStrippedRequest
484-
if (class_exists($setting)) {
489+
if (is_string($setting) && class_exists($setting)) {
485490
$setting = new $setting();
486491

487492
return is_callable($setting) ? $setting($request) : abort(500, get_class($setting).' is not invokable.');
488493
}
489494

490-
// if a closure was passed
491-
if (is_callable($setting)) {
492-
return $setting($request);
493-
}
494-
495495
return $request->only($this->getAllFieldNames());
496496
}
497497

tests/Unit/CrudPanel/CrudPanelFieldsTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Arr;
66
use Backpack\CRUD\Tests\Unit\Models\User;
7+
use Illuminate\Http\Request;
78

89
/**
910
* @covers Backpack\CRUD\app\Library\CrudPanel\Traits\Fields
@@ -598,4 +599,44 @@ public function testFieldNameIsPartialRelationInCrudModel()
598599
$field = $this->crudPanel->fields()['articles_id'];
599600
$this->assertEquals($field['relation_type'], 'HasMany');
600601
}
602+
603+
public function testGetStrippedSaveRequestWithClosure()
604+
{
605+
$this->crudPanel->setOperationSetting(
606+
'strippedRequest',
607+
static function (Request $request) {
608+
return $request->toArray();
609+
},
610+
'update'
611+
);
612+
$this->crudPanel->setOperation('update');
613+
$this->crudPanel->setModel(User::class);
614+
$request = request()->create('/users/1/edit', 'POST', ['name' => 'john']);
615+
$result = $this->crudPanel->getStrippedSaveRequest($request);
616+
$this->assertIsArray($result);
617+
$this->assertSame(['name' => 'john'], $result);
618+
}
619+
620+
public function testGetStrippedSaveRequestWithClass()
621+
{
622+
$this->crudPanel->setOperationSetting(
623+
'strippedRequest',
624+
Invokable::class,
625+
'update'
626+
);
627+
$this->crudPanel->setOperation('update');
628+
$this->crudPanel->setModel(User::class);
629+
$request = request()->create('/users/1/edit', 'POST', ['name' => 'john']);
630+
$result = $this->crudPanel->getStrippedSaveRequest($request);
631+
$this->assertIsArray($result);
632+
$this->assertSame(['invokable' => 'invokable'], $result);
633+
}
634+
}
635+
636+
class Invokable
637+
{
638+
public function __invoke(): array
639+
{
640+
return ['invokable' => 'invokable'];
641+
}
601642
}

0 commit comments

Comments
 (0)