Поле BelongsTo предназначено для работы с одноименной связью в Laravel и включает все Базовые методы.
BelongsTo::make(
Closure|string $label,
?string $relationName = null,
Closure|string|null $formatted = null,
ModelResource|string|null $resource = null,
)
BelongsTo::make(
Closure|string $label,
?string $relationName = null,
Closure|string|null $formatted = null,
ModelResource|string|null $resource = null,
)
BelongsTo::make(
Closure|string $label,
?string $relationName = null,
Closure|string|null $formatted = null,
ModelResource|string|null $resource = null,
)
BelongsTo::make(
Closure|string $label,
?string $relationName = null,
Closure|string|null $formatted = null,
ModelResource|string|null $resource = null,
)
BelongsTo::make(
Closure|string $label,
?string $relationName = null,
Closure|string|null $formatted = null,
ModelResource|string|null $resource = null,
)
$label - метка, заголовок поля,
$relationName - название связи,
$formatted - замыкание или поле в связанной таблице для отображения значений,
$resource - ModelResource, на которую ссылается связь.
Наличие ModelResource, на который ссылается отношение, обязательно.
Ресурс также необходимо зарегистрировать в сервис-провайдере MoonShineServiceProvider в методе $core->resources().
В противном случае будет ошибка 500.
use App\MoonShine\Resources\UserResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make(
'User',
'user',
resource: UserResource::class
)
namespaces
use App\MoonShine\Resources\UserResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make(
'User',
'user',
resource: UserResource::class
)
use App\MoonShine\Resources\UserResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make(
'User',
'user',
resource: UserResource::class
)
namespaces
use App\MoonShine\Resources\UserResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make(
'User',
'user',
resource: UserResource::class
)
use App\MoonShine\Resources\UserResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make(
'User',
'user',
resource: UserResource::class
)

Вы можете опустить $resource, если ModelResource совпадает с названием связи.
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('User', 'user')
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('User', 'user')
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('User', 'user')
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('User', 'user')
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('User', 'user')
Если не указать $relationName, то имя связи будет определено автоматически на основе $label (по правилам camelCase).
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('User')
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('User')
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('User')
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('User')
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('User')
По умолчанию для отображения значения используется поле в связанной таблице, которое указано свойством $column в ModelResource.
Аргумент $formatted позволяет переопределить свойство $column.
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make(
'User',
'user',
formatted: 'first_name'
)
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make(
'User',
'user',
formatted: 'first_name'
)
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make(
'User',
'user',
formatted: 'first_name'
)
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make(
'User',
'user',
formatted: 'first_name'
)
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make(
'User',
'user',
formatted: 'first_name'
)
Если необходимо указать более сложное значение для отображения, то в аргумент $formatted можно передать функцию обратного вызова.
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make(
'Country',
'country',
fn($item) => "$item->id. $item->title"
)
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make(
'Country',
'country',
fn($item) => "$item->id. $item->title"
)
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make(
'Country',
'country',
fn($item) => "$item->id. $item->title"
)
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make(
'Country',
'country',
fn($item) => "$item->id. $item->title"
)
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make(
'Country',
'country',
fn($item) => "$item->id. $item->title"
)
Если необходимо изменить колонку при работе с моделями, используйте метод onAfterFill().
use App\MoonShine\Resources\CategoryResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make(
'Category',
resource: CategoryResource::class
)
->afterFill(
fn($field) => $field->setColumn('changed_category_id')
)
namespaces
use App\MoonShine\Resources\CategoryResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make(
'Category',
resource: CategoryResource::class
)
->afterFill(
fn($field) => $field->setColumn('changed_category_id')
)
use App\MoonShine\Resources\CategoryResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make(
'Category',
resource: CategoryResource::class
)
->afterFill(
fn($field) => $field->setColumn('changed_category_id')
)
namespaces
use App\MoonShine\Resources\CategoryResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make(
'Category',
resource: CategoryResource::class
)
->afterFill(
fn($field) => $field->setColumn('changed_category_id')
)
use App\MoonShine\Resources\CategoryResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make(
'Category',
resource: CategoryResource::class
)
->afterFill(
fn($field) => $field->setColumn('changed_category_id')
)
Вы можете использовать метод default(), если вам нужно указать значение по умолчанию для поля.
default(mixed $default)
default(mixed $default)
default(mixed $default)
default(mixed $default)
default(mixed $default)
Вы должны передать объект модели в качестве значения по умолчанию.
use App\MoonShine\Resources\CategoryResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('Country', resource: CategoryResource::class)
->default(Country::find(1))
namespaces
use App\MoonShine\Resources\CategoryResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('Country', resource: CategoryResource::class)
->default(Country::find(1))
use App\MoonShine\Resources\CategoryResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('Country', resource: CategoryResource::class)
->default(Country::find(1))
namespaces
use App\MoonShine\Resources\CategoryResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('Country', resource: CategoryResource::class)
->default(Country::find(1))
use App\MoonShine\Resources\CategoryResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('Country', resource: CategoryResource::class)
->default(Country::find(1))
Как и для всех полей, если необходимо хранить NULL, нужно добавить метод nullable().
nullable(Closure|bool|null $condition = null)
nullable(Closure|bool|null $condition = null)
nullable(Closure|bool|null $condition = null)
nullable(Closure|bool|null $condition = null)
nullable(Closure|bool|null $condition = null)
use App\MoonShine\Resources\CategoryResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('Country', resource: CategoryResource::class)
->nullable()
namespaces
use App\MoonShine\Resources\CategoryResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('Country', resource: CategoryResource::class)
->nullable()
use App\MoonShine\Resources\CategoryResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('Country', resource: CategoryResource::class)
->nullable()
namespaces
use App\MoonShine\Resources\CategoryResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('Country', resource: CategoryResource::class)
->nullable()
use App\MoonShine\Resources\CategoryResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('Country', resource: CategoryResource::class)
->nullable()

Не забудьте указать в таблице базы данных, что поле может принимать значение Null.
Метод placeholder() позволяет установить атрибут placeholder у поля.
placeholder(string $value)
placeholder(string $value)
placeholder(string $value)
placeholder(string $value)
placeholder(string $value)
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('Country', 'country')
->nullable()
->placeholder('Country')
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('Country', 'country')
->nullable()
->placeholder('Country')
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('Country', 'country')
->nullable()
->placeholder('Country')
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('Country', 'country')
->nullable()
->placeholder('Country')
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('Country', 'country')
->nullable()
->placeholder('Country')
Если необходимо осуществлять поиск среди значений, нужно добавить метод searchable().
use App\MoonShine\Resources\CountryResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('Country', 'country', resource: CountryResource::class)
->searchable()
namespaces
use App\MoonShine\Resources\CountryResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('Country', 'country', resource: CountryResource::class)
->searchable()
use App\MoonShine\Resources\CountryResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('Country', 'country', resource: CountryResource::class)
->searchable()
namespaces
use App\MoonShine\Resources\CountryResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('Country', 'country', resource: CountryResource::class)
->searchable()
use App\MoonShine\Resources\CountryResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('Country', 'country', resource: CountryResource::class)
->searchable()
Метод creatable() позволяет создать новый объект связи через модальное окно.
creatable(
Closure|bool|null $condition = null,
?ActionButton $button = null,
)
creatable(
Closure|bool|null $condition = null,
?ActionButton $button = null,
)
creatable(
Closure|bool|null $condition = null,
?ActionButton $button = null,
)
creatable(
Closure|bool|null $condition = null,
?ActionButton $button = null,
)
creatable(
Closure|bool|null $condition = null,
?ActionButton $button = null,
)
BelongsTo::make('Author', resource: AuthorResource::class)
->creatable()
BelongsTo::make('Author', resource: AuthorResource::class)
->creatable()
BelongsTo::make('Author', resource: AuthorResource::class)
->creatable()
BelongsTo::make('Author', resource: AuthorResource::class)
->creatable()
BelongsTo::make('Author', resource: AuthorResource::class)
->creatable()

Вы можете настроить кнопку создания, передав параметр button в метод.
use App\MoonShine\Resources\AuthorResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
use MoonShine\UI\Components\ActionButton;
BelongsTo::make('Author', resource: AuthorResource::class)
->creatable(
button: ActionButton::make('Custom button', '')
)
namespaces
use App\MoonShine\Resources\AuthorResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
use MoonShine\UI\Components\ActionButton;
BelongsTo::make('Author', resource: AuthorResource::class)
->creatable(
button: ActionButton::make('Custom button', '')
)
use App\MoonShine\Resources\AuthorResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
use MoonShine\UI\Components\ActionButton;
BelongsTo::make('Author', resource: AuthorResource::class)
->creatable(
button: ActionButton::make('Custom button', '')
)
namespaces
use App\MoonShine\Resources\AuthorResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
use MoonShine\UI\Components\ActionButton;
BelongsTo::make('Author', resource: AuthorResource::class)
->creatable(
button: ActionButton::make('Custom button', '')
)
use App\MoonShine\Resources\AuthorResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
use MoonShine\UI\Components\ActionButton;
BelongsTo::make('Author', resource: AuthorResource::class)
->creatable(
button: ActionButton::make('Custom button', '')
)
Метод valuesQuery() позволяет изменить запрос для получения значений.
valuesQuery(Closure $callback)
valuesQuery(Closure $callback)
valuesQuery(Closure $callback)
valuesQuery(Closure $callback)
valuesQuery(Closure $callback)
use App\MoonShine\Resources\CategoryResource;
use Illuminate\Contracts\Database\Eloquent\Builder;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
use MoonShine\UI\Fields\Field;
BelongsTo::make('Category', 'category', resource: CategoryResource::class)
->valuesQuery(fn(Builder $query, Field $field) => $query->where('active', true))
namespaces
use App\MoonShine\Resources\CategoryResource;
use Illuminate\Contracts\Database\Eloquent\Builder;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
use MoonShine\UI\Fields\Field;
BelongsTo::make('Category', 'category', resource: CategoryResource::class)
->valuesQuery(fn(Builder $query, Field $field) => $query->where('active', true))
use App\MoonShine\Resources\CategoryResource;
use Illuminate\Contracts\Database\Eloquent\Builder;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
use MoonShine\UI\Fields\Field;
BelongsTo::make('Category', 'category', resource: CategoryResource::class)
->valuesQuery(fn(Builder $query, Field $field) => $query->where('active', true))
namespaces
use App\MoonShine\Resources\CategoryResource;
use Illuminate\Contracts\Database\Eloquent\Builder;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
use MoonShine\UI\Fields\Field;
BelongsTo::make('Category', 'category', resource: CategoryResource::class)
->valuesQuery(fn(Builder $query, Field $field) => $query->where('active', true))
use App\MoonShine\Resources\CategoryResource;
use Illuminate\Contracts\Database\Eloquent\Builder;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
use MoonShine\UI\Fields\Field;
BelongsTo::make('Category', 'category', resource: CategoryResource::class)
->valuesQuery(fn(Builder $query, Field $field) => $query->where('active', true))
Для реализации асинхронного поиска значений используйте метод asyncSearch().
asyncSearch(
string $column = null,
?Closure $searchQuery = null,
?Closure $formatted = null,
?string $associatedWith = null,
int $limit = 15,
?string $url = null,
)
asyncSearch(
string $column = null,
?Closure $searchQuery = null,
?Closure $formatted = null,
?string $associatedWith = null,
int $limit = 15,
?string $url = null,
)
asyncSearch(
string $column = null,
?Closure $searchQuery = null,
?Closure $formatted = null,
?string $associatedWith = null,
int $limit = 15,
?string $url = null,
)
asyncSearch(
string $column = null,
?Closure $searchQuery = null,
?Closure $formatted = null,
?string $associatedWith = null,
int $limit = 15,
?string $url = null,
)
asyncSearch(
string $column = null,
?Closure $searchQuery = null,
?Closure $formatted = null,
?string $associatedWith = null,
int $limit = 15,
?string $url = null,
)
use App\MoonShine\Resources\CategoryResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('Category', 'category', resource: CategoryResource::class)
->asyncSearch()
namespaces
use App\MoonShine\Resources\CategoryResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('Category', 'category', resource: CategoryResource::class)
->asyncSearch()
use App\MoonShine\Resources\CategoryResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('Category', 'category', resource: CategoryResource::class)
->asyncSearch()
namespaces
use App\MoonShine\Resources\CategoryResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('Category', 'category', resource: CategoryResource::class)
->asyncSearch()
use App\MoonShine\Resources\CategoryResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('Category', 'category', resource: CategoryResource::class)
->asyncSearch()
Поиск будет осуществляться по полю связи ресурса column.
По умолчанию column=id.
Вы можете передать параметры в метод asyncSearch():
$column - поле, по которому осуществляется поиск,
$searchQuery - функция обратного вызова для фильтрации значений,
$formatted - функция обратного вызова для настройки вывода,
$associatedWith - поле, с которым установить связь,
$limit - количество элементов в результатах поиска,
$url - url для обработки асинхронного запроса.
use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use MoonShine\UI\Fields\Field;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('Category', 'category', resource: CategoryResource::class)
->asyncSearch(
'title',
searchQuery: function (Builder $query, Request $request, string $term, Field $field) {
return $query->where('id', '!=', 2);
},
formatted: function ($country, Field $field) {
return $country->id . ' | ' . $country->title;
},
limit: 10,
url: 'https://moonshine-laravel.com/async'
)
namespaces
use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use MoonShine\UI\Fields\Field;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('Category', 'category', resource: CategoryResource::class)
->asyncSearch(
'title',
searchQuery: function (Builder $query, Request $request, string $term, Field $field) {
return $query->where('id', '!=', 2);
},
formatted: function ($country, Field $field) {
return $country->id . ' | ' . $country->title;
},
limit: 10,
url: 'https://moonshine-laravel.com/async'
)
use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use MoonShine\UI\Fields\Field;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('Category', 'category', resource: CategoryResource::class)
->asyncSearch(
'title',
searchQuery: function (Builder $query, Request $request, string $term, Field $field) {
return $query->where('id', '!=', 2);
},
formatted: function ($country, Field $field) {
return $country->id . ' | ' . $country->title;
},
limit: 10,
url: 'https://moonshine-laravel.com/async'
)
namespaces
use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use MoonShine\UI\Fields\Field;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('Category', 'category', resource: CategoryResource::class)
->asyncSearch(
'title',
searchQuery: function (Builder $query, Request $request, string $term, Field $field) {
return $query->where('id', '!=', 2);
},
formatted: function ($country, Field $field) {
return $country->id . ' | ' . $country->title;
},
limit: 10,
url: 'https://moonshine-laravel.com/async'
)
use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use MoonShine\UI\Fields\Field;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('Category', 'category', resource: CategoryResource::class)
->asyncSearch(
'title',
searchQuery: function (Builder $query, Request $request, string $term, Field $field) {
return $query->where('id', '!=', 2);
},
formatted: function ($country, Field $field) {
return $country->id . ' | ' . $country->title;
},
limit: 10,
url: 'https://moonshine-laravel.com/async'
)
При построении запроса в searchQuery можно использовать текущие значения формы.
Для этого необходимо передать Request в функцию обратного вызова.
use App\MoonShine\Resources\CityResource;
use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
use MoonShine\UI\Fields\Field;
use MoonShine\UI\Fields\Select;
Select::make('Country', 'country_id'),
BelongsTo::make('City', 'city', resource: CityResource::class)
->asyncSearch(
'title',
searchQuery: function (Builder $query, Request $request, string $term, Field $field): Builder {
return $query->where('country_id', $request->get('country_id'));
}
)
namespaces
use App\MoonShine\Resources\CityResource;
use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
use MoonShine\UI\Fields\Field;
use MoonShine\UI\Fields\Select;
Select::make('Country', 'country_id'),
BelongsTo::make('City', 'city', resource: CityResource::class)
->asyncSearch(
'title',
searchQuery: function (Builder $query, Request $request, string $term, Field $field): Builder {
return $query->where('country_id', $request->get('country_id'));
}
)
use App\MoonShine\Resources\CityResource;
use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
use MoonShine\UI\Fields\Field;
use MoonShine\UI\Fields\Select;
Select::make('Country', 'country_id'),
BelongsTo::make('City', 'city', resource: CityResource::class)
->asyncSearch(
'title',
searchQuery: function (Builder $query, Request $request, string $term, Field $field): Builder {
return $query->where('country_id', $request->get('country_id'));
}
)
namespaces
use App\MoonShine\Resources\CityResource;
use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
use MoonShine\UI\Fields\Field;
use MoonShine\UI\Fields\Select;
Select::make('Country', 'country_id'),
BelongsTo::make('City', 'city', resource: CityResource::class)
->asyncSearch(
'title',
searchQuery: function (Builder $query, Request $request, string $term, Field $field): Builder {
return $query->where('country_id', $request->get('country_id'));
}
)
use App\MoonShine\Resources\CityResource;
use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
use MoonShine\UI\Fields\Field;
use MoonShine\UI\Fields\Select;
Select::make('Country', 'country_id'),
BelongsTo::make('City', 'city', resource: CityResource::class)
->asyncSearch(
'title',
searchQuery: function (Builder $query, Request $request, string $term, Field $field): Builder {
return $query->where('country_id', $request->get('country_id'));
}
)
Для установления связи значений выбора между полями можно использовать метод associatedWith().
associatedWith(string $column, ?Closure $searchQuery = null)
associatedWith(string $column, ?Closure $searchQuery = null)
associatedWith(string $column, ?Closure $searchQuery = null)
associatedWith(string $column, ?Closure $searchQuery = null)
associatedWith(string $column, ?Closure $searchQuery = null)
$column - поле, с которым устанавливается связь,
$searchQuery - функция обратного вызова для фильтрации значений.
use App\MoonShine\Resources\CityResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('City', 'city', resource: CityResource::class)
->associatedWith('country_id')
namespaces
use App\MoonShine\Resources\CityResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('City', 'city', resource: CityResource::class)
->associatedWith('country_id')
use App\MoonShine\Resources\CityResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('City', 'city', resource: CityResource::class)
->associatedWith('country_id')
namespaces
use App\MoonShine\Resources\CityResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('City', 'city', resource: CityResource::class)
->associatedWith('country_id')
use App\MoonShine\Resources\CityResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('City', 'city', resource: CityResource::class)
->associatedWith('country_id')
Если необходимо сразу же после отображения страницы отправить запрос на значения, тогда необходимо добавить метод asyncOnInit(whenOpen: false).
BelongsTo::make('City', 'city', resource: CityResource::class)
->associatedWith('country_id')
->asyncOnInit(whenOpen: false)
BelongsTo::make('City', 'city', resource: CityResource::class)
->associatedWith('country_id')
->asyncOnInit(whenOpen: false)
BelongsTo::make('City', 'city', resource: CityResource::class)
->associatedWith('country_id')
->asyncOnInit(whenOpen: false)
BelongsTo::make('City', 'city', resource: CityResource::class)
->associatedWith('country_id')
->asyncOnInit(whenOpen: false)
BelongsTo::make('City', 'city', resource: CityResource::class)
->associatedWith('country_id')
->asyncOnInit(whenOpen: false)
При пустом asyncOnInit() или asyncOnInit(whenOpen: true) запрос будет отправляться после клика на BelongsTo.
Для более сложной настройки вы можете использовать asyncSearch().
Метод withImage() позволяет добавить изображение к значению.
withImage(
string $column,
string $disk = 'public',
string $dir = ''
)
withImage(
string $column,
string $disk = 'public',
string $dir = ''
)
withImage(
string $column,
string $disk = 'public',
string $dir = ''
)
withImage(
string $column,
string $disk = 'public',
string $dir = ''
)
withImage(
string $column,
string $disk = 'public',
string $dir = ''
)
$column - поле с изображением,
$disk - диск файловой системы,
$dir - директория относительно корня диска.
use App\MoonShine\Resources\CountryResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('Country', resource: CountryResource::class)
->withImage('thumb', 'public', 'countries')
namespaces
use App\MoonShine\Resources\CountryResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('Country', resource: CountryResource::class)
->withImage('thumb', 'public', 'countries')
use App\MoonShine\Resources\CountryResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('Country', resource: CountryResource::class)
->withImage('thumb', 'public', 'countries')
namespaces
use App\MoonShine\Resources\CountryResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('Country', resource: CountryResource::class)
->withImage('thumb', 'public', 'countries')
use App\MoonShine\Resources\CountryResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('Country', resource: CountryResource::class)
->withImage('thumb', 'public', 'countries')

Все опции выбора доступны для изменения через атрибуты data:
use App\MoonShine\Resources\CountryResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('Country', resource: CountryResource::class)
->searchable()
->customAttributes([
'data-search-result-limit' => 5
])
namespaces
use App\MoonShine\Resources\CountryResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('Country', resource: CountryResource::class)
->searchable()
->customAttributes([
'data-search-result-limit' => 5
])
use App\MoonShine\Resources\CountryResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('Country', resource: CountryResource::class)
->searchable()
->customAttributes([
'data-search-result-limit' => 5
])
namespaces
use App\MoonShine\Resources\CountryResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('Country', resource: CountryResource::class)
->searchable()
->customAttributes([
'data-search-result-limit' => 5
])
use App\MoonShine\Resources\CountryResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('Country', resource: CountryResource::class)
->searchable()
->customAttributes([
'data-search-result-limit' => 5
])
Для получения более подробной информации, пожалуйста, обратитесь к Choices.
Метод native() отключает библиотеку Choices.js и отображает выбор в нативном режиме.
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('Type')->native()
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('Type')->native()
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('Type')->native()
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('Type')->native()
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make('Type')->native()
По умолчанию BelongsTo ссылается на страницу редактирования.
Вы можете переопределить это поведение с помощью метода link().
use App\MoonShine\Resources\CategoryResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make(
'Category',
'category',
resource: CategoryResource::class,
)
->link(
link: fn(string $value, BelongsTo $ctx) => $ctx->getResource()->getDetailPageUrl($ctx->getData()->getKey()),
name: fn(string $value) => $value,
icon: 'users',
blank: true,
)
namespaces
use App\MoonShine\Resources\CategoryResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make(
'Category',
'category',
resource: CategoryResource::class,
)
->link(
link: fn(string $value, BelongsTo $ctx) => $ctx->getResource()->getDetailPageUrl($ctx->getData()->getKey()),
name: fn(string $value) => $value,
icon: 'users',
blank: true,
)
use App\MoonShine\Resources\CategoryResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make(
'Category',
'category',
resource: CategoryResource::class,
)
->link(
link: fn(string $value, BelongsTo $ctx) => $ctx->getResource()->getDetailPageUrl($ctx->getData()->getKey()),
name: fn(string $value) => $value,
icon: 'users',
blank: true,
)
namespaces
use App\MoonShine\Resources\CategoryResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make(
'Category',
'category',
resource: CategoryResource::class,
)
->link(
link: fn(string $value, BelongsTo $ctx) => $ctx->getResource()->getDetailPageUrl($ctx->getData()->getKey()),
name: fn(string $value) => $value,
icon: 'users',
blank: true,
)
use App\MoonShine\Resources\CategoryResource;
use MoonShine\Laravel\Fields\Relationships\BelongsTo;
BelongsTo::make(
'Category',
'category',
resource: CategoryResource::class,
)
->link(
link: fn(string $value, BelongsTo $ctx) => $ctx->getResource()->getDetailPageUrl($ctx->getData()->getKey()),
name: fn(string $value) => $value,
icon: 'users',
blank: true,
)