A
A
Alexander Frolov2019-10-07 12:13:31
Laravel
Alexander Frolov, 2019-10-07 12:13:31

How to set an array in the Select tag by default?

Good afternoon. A question. Project on Laravel. There is a form in it select, where you need to select one of several organizations, but also to be able to select all organizations at once and save it all in the database.
With a choice of 1 organization and saving, everything is selected, saved. But if there are several .. I can not figure out how to do it.
I did it through the usual Select tags, and through the Laravel Collective.
Tell me how to cram all the data into the field and save them.
Controller:

public function create()
    {
        $title = 'Добавление нового терминала';
//        $allOrganization = Organization::all()->where('user_id', Auth::user()->id);

//        dd($allOrganization);
        $allTerminals = Terminal::all();
        $terminalOrganizationId = Organization::where('user_id', Auth::user()->id)->pluck('name', 'id')->all();
        $terminalOrganizationIdJson = json_encode($terminalOrganizationId);
//        dd($terminalOrganizationIdJson);
//        $terminalCashboxId = Cashbox::where('user_id', Auth::user()->id)->pluck('checking_account', 'id')->all();
        return view('client.terminals.create', compact('title', 'allTerminals',
            'terminalOrganizationId', 'allOrganization', 'terminalOrganizationIdJson'));
    }

    //Валидация полей и создание терминала в БД
    public function store(Request $request)
    {

        $this->validate($request, [
            'name' => 'required',
            'organization_id' => 'required',
            'password' => 'required|min:4',
            'confirmPassword' => 'required|same:password'
        ]);

        $terminal = new Terminal();
        $terminal->name = $request->name;
        $terminal->uuid = (string) Str::uuid();
        $terminal->organization_id = $request->organization_id;
//        $terminal->cashbox_id = $request->cashbox_id;
        $terminal->installation_address = $request->installation_address;
        $terminal->acquiring = $request->acquiring;
        $terminal->fiscalization = $request->fiscalization;
        $terminal->password = bcrypt($request->password);
//        $terminal->confirmPassword = bcrypt($request->confirmPassword);
        $terminal->user_id = Auth::user()->id;
            $terminal->save();


        return redirect()->route('terminals.index');
    }

View:
{{--<div class="form-group form-group-add-terminal col-md-11">--}}
                            {{--<label for="organization_id">Организация</label>--}}
                            {{--{{Form::select('organization_id', $terminalOrganizationId, null,['placeholder' => 'Введите организацию', 'class' => 'form-control custom-select'])}}--}}
                        {{--</div>--}}

                        <div class="form-group form-group-add-terminal col-md-11">
                        <label for="organization_id">Организация</label>
                            <select class="form-control custom-select" name="country_id" >
                                <option>
                                {{$terminalOrganizationIdJson}}
                                </option>
                                @foreach($terminalOrganizationId as $item)
                                    <option >
                                        {{$item}}
                                    </option>
                                @endforeach
                            </select>
                        </div>

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
Eduard Bazhenov, 2019-10-07
@kip_34

If I understood correctly, then you need a "many-to-many" relationship, so that you can select more than one organization, you need another plate that will contain the terminal id and organization id.

N
Newn, 2019-10-07
@Newn

I understood correctly in the html form you need to draw the output of all organizations? And it turns out only 1?

O
Ordec, 2019-10-07
@Ordec

Make the option "All" and on the server add a handler for it

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question