How to take a value from a select (Edit) Laravel

Asked

Viewed 1,284 times

0

good morning, someone can help me in this select, (example: User will register and there he selects the customer already registered).

then you will have the inputs to fill but select, OK!

now it will change the form, as I can bring the selected value

edit.blade.php

@section('content')
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-8">
                <div class="card">
                    <div class="card-header">
                        <h2>Alterar Apontamento</h2>
                    </div>
                    <div class="card-body">
                        <form action="{{route('equipe.alterar.apontamento', ['id' => $apontamento->id])}}" method="post">
                        @csrf <!--protecao-->
                            @method('put')
                            <div class="form-group">
                                <label for="cliente">Cliente</label>
                                <select name="cliente" id="cliente" class="form-control" >
                                    <option value="" selected>Selecione</option>
                                    @foreach($clientes as $cliente)
                                        <option  value="{{$cliente->id}}">{{$cliente->user->name}}</option>
                                    @endforeach
                                </select>
                                @error('cliente')
                                <div class="alert alert-danger">{{ $message }}</div>
                                @enderror
                            </div>
                            <div class="form-group">
                                <label for="servicos">Serviços</label>
                                <select name="servicos" id="servicos" class="form-control">
                                    <option value="" selected>Selecione</option>
                                    @foreach($servicos as $servico)
                                        <option value="{{$servico->id}}">{{$servico->nome}}</option>
                                    @endforeach
                                </select>
                                @error('servicos')
                                <div class="alert alert-danger">{{ $message }}</div>
                                @enderror
                            </div>
                            <div class="form-group">
                                <label for="inicio">Hora inicio:</label>
                                <input type="text" class="form-control" name="inicio" id="inicio" value="{{$apontamento->inicio}}">
                                @error('inicio')
                                <div class="alert alert-danger">{{ $message }}</div>
                                @enderror
                            </div>
                            <div class="form-group">
                                <label for="termino">Término:</label>
                                <input type="text" class="form-control" name="termino" id="termino" value="{{$apontamento->termino}}">
                                @error('termino')
                                <div class="alert alert-danger">{{ $message }}</div>
                                @enderror
                            </div>
                            <div class="form-group">
                                <label for="considerado">Considerado:</label>
                                <input type="text" class="form-control" name="considerado" id="considerado" value="{{$apontamento->considerado}}">
                                @error('considerado')
                                <div class="alert alert-danger">{{ $message }}</div>
                                @enderror
                            </div>
                            <div class="form-group">
                                <label for="atividade">Atividade:</label>
                                <input type="text" class="form-control" name="atividade" id="atividade" value="{{$apontamento->atividade}}">
                                @error('atividade')
                                <div class="alert alert-danger">{{ $message }}</div>
                                @enderror
                            </div>
                            <div class="form-group">
                                <label for="observacao">Observação</label>
                                <textarea type="text" rows="6" class="form-control" name="observacao" id="observacao" value="{{$apontamento->observacao}}"></textarea>
                                @error('observacao')
                                <div class="alert alert-danger">{{ $message }}</div>
                                @enderror
                            </div>

                            <button class="btn btn-primary">Alterar</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection

controller

 public function edit($id)
    {
        $apontamento = Apontamento::with('user')->where('id', $id)->first();
        return view('apontamento.editar', compact('apontamento'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $apontamento = Apontamento::find($id);
        $clientes = Cliente::find();
        $servicos = Servicos::find();

        $apontamento->atividade = $request->atividade;
        $apontamento->inicio = $request->inicio;
        $apontamento->termino = $request->termino;
        $apontamento->considerado = $request->considerado;
        $apontamento->observacao = $request->observacao;
        $clientes->cliente_id = $request->cliente;
        $servicos->servico_id = $request->servicos;
            $apontamento->save();

        return redirect()->back();
    }

1 answer

1

You need to compare the value that stores the selected client in the registration and compare with the values that are generating this SELECT and add attribute selected to position, example:

 @if ($cliente->id === $apontamento->cliente_id) { 'selected'; } @endif

Complete:

<div class="form-group">
    <label for="cliente">Cliente</label>
    <select name="cliente" id="cliente" class="form-control" >
        <option value="" selected>Selecione</option>
        @foreach($clientes as $cliente)
            <option  value="{{$cliente->id}}" @if ($cliente->id === $apontamento->cliente_id) { 'selected'; } @endif >{{$cliente->user->name}}</option>
        @endforeach
    </select>
    @error('cliente')
    <div class="alert alert-danger">{{ $message }}</div>
    @enderror
</div>
  • see if my controller is right..

  • can, changed controller $clients = Client:::all();

  • @Fernandosqse did not understand

  • it worked rsrsrs

  • So if it’s useful @Fernandosqse signal as an answer to your question

Browser other questions tagged

You are not signed in. Login or sign up in order to post.