Answer the question
In order to leave comments, you need to log in
Laravel-admin doesn't render in belongsToMany form, what are the options?
There is a Promo model:
protected $table = 'promo';
// ...
public function locations()
{
return $this->belongsToMany(Cities::class, 'cities_promo');
}
protected function form()
{
$location = Cities::pluck('name', 'id');
$form = new Form(new Promo);
$form->text('title', __('Наименование'));
$form->textarea('desc', __('Описание'));
$form->multipleSelect('locations')->options($location);
return $form;
}
Answer the question
In order to leave comments, you need to log in
An intermediate solution was to use an attribute.
It is necessary that the format for multipleSelect (and others) be in the array format [1,2,3 ... ,7].
During normal communication,
{
['id' => 1,
'name' => 'Moscow',
...
],
['id' => 2,
'name' => 'Ekb',
...
],
}
...
//Add extra attribute
//These attributes will be written to the database, if you do not want this, then do not advertise!
//protected $attributes = ['cities'];
//Make it available in the json response
protected $appends = ['cities'];
public function getCitiesAttribute()
{
return $this->locations->pluck('id');
}
public function setCitiesAttribute($value)
{
$this->locations()->sync($value);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question