Skip to content
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

[3.x] HasMany modifyBuilder #1350

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
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
22 changes: 18 additions & 4 deletions src/Laravel/src/Fields/Relationships/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,14 +340,28 @@
*/
protected function getTablePreview(): TableBuilderContract
{
$items = $this->toValue();
$resource = $this->getResource();

if(\is_null($this->modifyBuilder) && $resource->getWith() === []) {
$items = $this->toValue();
} else {
// In the preview mode of the parent resource on the IndexPage, when retrieving elements,
// the $with and $modifyBuilder of the current HasMany resource are not taken into account.
// If the HasMany resource has $with or $modifyBuilder specified,
// it is necessary to re-execute the query to retrieve all items of the current HasMany.

if(! \is_null($this->modifyBuilder)) {
$casted = $this->getRelatedModel();
$relation = $casted?->{$this->getRelationName()}();
$resource->customQueryBuilder(value($this->modifyBuilder, $relation, true));

Check failure on line 356 in src/Laravel/src/Fields/Relationships/HasMany.php

View workflow job for this annotation

GitHub Actions / moonshine-analyse

Parameter #1 $builder of method MoonShine\Laravel\Resources\ModelResource<Illuminate\Database\Eloquent\Model,MoonShine\Contracts\Core\CrudPageContract,MoonShine\Contracts\Core\CrudPageContract,MoonShine\Contracts\Core\CrudPageContract>::customQueryBuilder() expects Illuminate\Contracts\Database\Eloquent\Builder, Closure given.
}
$items = $resource->getItems();
}

if (filled($items)) {
$items = $items->take($this->getLimit());
}

$resource = $this->getResource();

return TableBuilder::make(items: $items)
->fields($this->getFieldsOnPreview())
->cast($resource->getCaster())
Expand Down Expand Up @@ -529,7 +543,7 @@
$resource->customQueryBuilder(
\is_null($this->modifyBuilder)
? $relation
: value($this->modifyBuilder, $relation)
: value($this->modifyBuilder, $relation, false)
);

$items = $resource->getItems();
Expand Down
37 changes: 37 additions & 0 deletions tests/Feature/Fields/Relationships/HasManyFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
uses()->group('model-relation-fields');
uses()->group('has-many-field');

use Illuminate\Database\Eloquent\Relations\Relation;
use MoonShine\Laravel\Fields\Relationships\HasMany;
use MoonShine\Laravel\Pages\Crud\FormPage;
use MoonShine\Laravel\Pages\Crud\IndexPage;
use MoonShine\Tests\Fixtures\Models\Comment;
use MoonShine\Tests\Fixtures\Models\Item;
use MoonShine\Tests\Fixtures\Resources\TestCommentResource;
use MoonShine\Tests\Fixtures\Resources\TestResourceBuilder;
Expand Down Expand Up @@ -163,3 +165,38 @@
expect($hasMany->getResource()->getItemID())
->toBeNull();
});

it('modify builder', function () {
$item = createItem(countComments: 2);

$comments = Comment::query()->get();

$commentFirst = $comments->first();
$commentLast = $comments->last();

$resource = TestResourceBuilder::new(Item::class)->setTestFields([
ID::make(),
Text::make('Name'),
HasMany::make('Comments title', 'comments', resource: TestCommentResource::class)
->modifyBuilder(
fn (Relation $relation, bool $preview) => $relation->where('id', $commentFirst->id)
)
,
]);

asAdmin()
->get($this->moonshineCore->getRouter()->getEndpoints()->toPage(page: IndexPage::class, resource: $resource))
->assertOk()
->assertSee('Comments title')
->assertSee($commentFirst->content)
->assertDontSee($commentLast->content)
;

asAdmin()
->get($this->moonshineCore->getRouter()->getEndpoints()->toPage(page: FormPage::class, resource: $resource, params: ['resourceItem' => $item->id]))
->assertOk()
->assertSee('Comments title')
->assertSee($commentFirst->content)
->assertDontSee($commentLast->content)
;
});
Loading