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

4.x throw exception if relationship is not found #14334

Open
wants to merge 6 commits into
base: 4.x
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion packages/forms/resources/js/components/file-upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ export default function fileUploadFormComponent({
.map((file) =>
file.source instanceof File
? file.serverId
: this.uploadedFileIndex[file.source] ?? null,
: (this.uploadedFileIndex[file.source] ?? null),
) // file.serverId is null for a file that is not yet uploaded
.filter((fileKey) => fileKey)

Expand Down
10 changes: 8 additions & 2 deletions packages/forms/src/Components/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -1157,15 +1157,17 @@ public function getLabel(): string | Htmlable | null

public function getRelationship(): BelongsTo | BelongsToMany | HasOneOrMany | HasManyThrough | BelongsToThrough | null
{
if (blank($this->getRelationshipName())) {
if (! $this->hasRelationship()) {
return null;
}

$record = $this->getModelInstance();

$relationship = null;

foreach (explode('.', $this->getRelationshipName()) as $nestedRelationshipName) {
$relationshipName = $this->getRelationshipName();

foreach (explode('.', $relationshipName) as $nestedRelationshipName) {
if (! $record->isRelation($nestedRelationshipName)) {
$relationship = null;

Expand All @@ -1176,6 +1178,10 @@ public function getRelationship(): BelongsTo | BelongsToMany | HasOneOrMany | Ha
$record = $relationship->getRelated();
}

if (! $relationship) {
throw new Exception("The relationship [{$relationshipName}] does not exist on the model [{$this->getModel()}].");
}

return $relationship;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
}

if (
(@js($this instanceof \Filament\Actions\Contracts\HasActions) ? $wire.mountedActions?.length ?? 0 : 0) &&
(@js($this instanceof \Filament\Actions\Contracts\HasActions) ? ($wire.mountedActions?.length ?? 0) : 0) &&
!$wire?.__instance?.effects?.redirect
) {
event.preventDefault()
Expand Down
13 changes: 11 additions & 2 deletions packages/tables/src/Filters/Concerns/HasRelationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Filament\Tables\Filters\Concerns;

use Closure;
use Exception;
use Filament\Support\Services\RelationshipJoiner;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
Expand Down Expand Up @@ -49,11 +50,15 @@ public function queriesRelationships(): bool

public function getRelationship(): Relation | Builder
{
$record = app($this->getTable()->getModel());
$model = $this->getTable()->getModel();

$record = app($model);

$relationship = null;

foreach (explode('.', $this->getRelationshipName()) as $nestedRelationshipName) {
$relationshipName = $this->getRelationshipName();

foreach (explode('.', $relationshipName) as $nestedRelationshipName) {
if (! $record->isRelation($nestedRelationshipName)) {
$relationship = null;

Expand All @@ -64,6 +69,10 @@ public function getRelationship(): Relation | Builder
$record = $relationship->getRelated();
}

if (! $relationship) {
throw new Exception("The relationship [{$relationshipName}] does not exist on the model [{$model}].");
}

return $relationship;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/tables/src/Filters/SelectFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public function multiple(bool | Closure $condition = true): static
}

/**
* @param bool | array<string> | Closure $condition
* @param bool | array<string> | Closure $condition
*/
public function searchable(bool | array | Closure $condition = true): static
{
Expand Down
Loading