Skip to content

Commit

Permalink
fixed issue #33
Browse files Browse the repository at this point in the history
  • Loading branch information
lee-to committed Sep 5, 2022
1 parent 1974049 commit 1656387
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
12 changes: 9 additions & 3 deletions src/Fields/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,23 @@
namespace Leeto\MoonShine\Fields;

use Illuminate\Database\Eloquent\Model;
use Leeto\MoonShine\Contracts\Fields\Relationships\HasRelationship;
use Leeto\MoonShine\Contracts\Fields\Relationships\BelongsToRelation;
use Leeto\MoonShine\Traits\Fields\WithRelationship;
use Leeto\MoonShine\Contracts\Fields\Relationships\HasRelationship;
use Leeto\MoonShine\Traits\Fields\Searchable;
use Leeto\MoonShine\Traits\Fields\WithRelationship;

class BelongsTo extends Field implements HasRelationship, BelongsToRelation
{
use Searchable, WithRelationship;
use Searchable;
use WithRelationship;

protected static string $view = 'select';

public function isMultiple(): bool
{
return false;
}

public function save(Model $item): Model
{
return $item->{$this->relation()}()
Expand Down
7 changes: 5 additions & 2 deletions src/Filters/BelongsToFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@

namespace Leeto\MoonShine\Filters;

use Leeto\MoonShine\Contracts\Fields\Relationships\HasRelationship;
use Leeto\MoonShine\Contracts\Fields\Relationships\BelongsToRelation;
use Leeto\MoonShine\Contracts\Fields\Relationships\HasRelationship;
use Leeto\MoonShine\Traits\Fields\CanBeMultiple;
use Leeto\MoonShine\Traits\Fields\Searchable;
use Leeto\MoonShine\Traits\Fields\WithRelationship;

class BelongsToFilter extends Filter implements HasRelationship, BelongsToRelation
{
use Searchable, WithRelationship;
use CanBeMultiple;
use Searchable;
use WithRelationship;

public static string $view = 'select';
}
18 changes: 12 additions & 6 deletions src/Filters/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

use Leeto\MoonShine\Contracts\Fields\HasAssets;
use Leeto\MoonShine\Contracts\Fields\HasFormViewValue;
use Leeto\MoonShine\Contracts\HtmlViewable;

use Leeto\MoonShine\Traits\Fields\FormElement;
use Leeto\MoonShine\Traits\Fields\ShowWhen;
use Leeto\MoonShine\Traits\Fields\WithHtmlAttributes;
Expand Down Expand Up @@ -38,12 +36,20 @@ public function name(string $index = null): string

public function getQuery(Builder $query): Builder
{
if ($this->hasRelationship() && !$this->belongToOne()) {
$table = $this->getRelated($query->getModel())->getTable();
if ($this->hasRelationship()) {
$related = $this->getRelated($query->getModel());

return $this->requestValue()
? $query->whereHas($this->relation(), function (Builder $q) use ($table) {
return $q->whereIn("$table.id", $this->requestValue());
? $query->whereHas($this->relation(), function (Builder $q) use ($related) {
$table = $related->getTable();
$id = $related->getKeyName();

return $q->whereIn(
"$table.$id",
is_array($this->requestValue())
? $this->requestValue()
: [$this->requestValue()]
);
})
: $query;
}
Expand Down
4 changes: 4 additions & 0 deletions src/Traits/Fields/WithRelationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public function isSelected(Model $item, string $value): bool
: in_array($value, $this->formViewValue($item));
}

if ($this->belongToOne() && is_array($this->formViewValue($item))) {
return in_array($value, $this->formViewValue($item));
}

return (string) $this->formViewValue($item) === $value
|| (!$this->formViewValue($item) && (string) $this->getDefault() === $value);
}
Expand Down

0 comments on commit 1656387

Please sign in to comment.