0

Model - Promo:

...
    protected $table = 'promo';
...
    public function locations()
    {
        return $this->belongsToMany(Cities::class, 'cities_promo');
    }

Controller in laravel-admin

...
    protected function form()
    {
        $location = Cities::pluck('name', 'id');

        $form = new Form(new Promo);

        $form->text('title', __('Title'));
        $form->textarea('desc', __('Description'));
        $form->multipleSelect('locations')->options($location);

        return $form;
    }
...

The bottom line is that it does not display the values that were previously selected and saved. An empty field is displayed there, where you can select values from the City model.

2 Answers 2

1

An intermediate solution was to use the attribute. It is necessary that the format for multipleSelect (and others) was in array format [1,2,3 ... ,7].

In normal communication, an array of the form is transmitted:

{
['id' => 1,
'name' => 'Moscow',
...
],
['id' => 2,
'name' => 'Ekb',
...
],
}

Therefore, for formalization, I used a third-party attribute "Cities" to the model "Promo".

...
    //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);
    }

If there are other suggestions, I am ready to listen. Thank.

Sign up to request clarification or add additional context in comments.

Comments

0

change $location to

$location = Cities::All()->pluck('name', 'id');

you can return $location to know it has value or not

also you can set options manually

$form->multipleSelect('locations')->options([1 => 'foo', 2 => 'bar', 'val' => 'Option name']);

to know it works

1 Comment

Yes, the recording is in success. But after saving, when editing data is not displayed. (There is a connection in the database).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.