Skip to content

Commit

Permalink
set model relation (#3219)
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmastech authored Mar 29, 2023
1 parent c135859 commit fbdcba9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/InteractsWithMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,9 @@ public function loadMedia(string $collectionName): Collection
return $collection
->filter(fn (Media $mediaItem) => $mediaItem->collection_name === $collectionName)
->sortBy('order_column')
->values();
->values()
->each
->setRelation('model', $this);
}

public function prepareToAttachMedia(Media $media, FileAdder $fileAdder): void
Expand Down
4 changes: 2 additions & 2 deletions tests/Feature/FileAdder/MediaConversions/DeleteMediaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@
$this->testModelWithoutMediaConversions = $this->testModelWithoutMediaConversions->fresh();

expect($this->testModelWithoutMediaConversions->getMedia('default'))->toHaveCount(3);
expect($excludedMedia[0])->toEqual($this->testModelWithoutMediaConversions->getMedia('images')[0]);
expect($excludedMedia[1])->toEqual($this->testModelWithoutMediaConversions->getMedia('images')[1]);
expect($excludedMedia[0]->withoutRelations())->toEqual($this->testModelWithoutMediaConversions->getMedia('images')[0]->withoutRelations());
expect($excludedMedia[1]->withoutRelations())->toEqual($this->testModelWithoutMediaConversions->getMedia('images')[1]->withoutRelations());
});

it('provides a chainable method for clearing a collection', function () {
Expand Down
14 changes: 14 additions & 0 deletions tests/Feature/InteractsWithMedia/GetMediaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,20 @@
->toArray());
});

it('will set model relation', function() {
DB::enableQueryLog();

$this->testModel->loadMedia('images');
expect(DB::getQueryLog())->toHaveCount(1);
$this->testModel->media->each(function(Media $media) {
expect($media->model)->toBeInstanceOf($this->testModel::class);
});

expect(DB::getQueryLog())->toHaveCount(1);

DB::DisableQueryLog();
});

it('will cache loaded media', function () {
DB::enableQueryLog();

Expand Down

0 comments on commit fbdcba9

Please sign in to comment.