Laravel: Fill in form::model

Asked

Viewed 968 times

1

I use the same form to register and edit, so I have that if else to show the form. As I have the table Convenio and Endereco, need to bring the information from the address through a with, my question is how to fill out the form automatically with this information.

The data relating to the table Convenio, are normally filled in, but in the table with with no. There in the form::model if I change the $convenio for $convenio->endereco, fills the fields for this table, but does not fill the fields of the table.

Would you have some way of putting the variable $convenio and $convenio->endereco together in the form? I tried a few tricks, but they didn’t work.

If I leave the name of input as array, as endereco[rua], works, but error when registering.

CONTROLLER

$convenio = Convenio::with('endereco')
                        ->with('telefones')
                        ->find($id);

$cidades = Cidade::pluck('nomeCidade', 'id');

$servicos = Servico::pluck('nomeServ', 'id')->all();

$title = 'Editar convênio';

return view('painel.convenio.create-edit', compact('convenio', 'cidades', 'servicos', 'title'));

VIEW

@if(isset($convenio))
            {!! Form::model($convenio, ['route' => ['convenio.update', $convenio->id], 'method' => 'put']) !!}
        @else
            {!! Form::open(['route' => 'convenio.store']) !!}
        @endif
            <div class="row form-group">
                <div class="col-sm-12">
                    {!! Form::label('nome', 'Nome') !!}
                    {!! Form::input('text', 'nome', null, ['id' => 'nome', 'placeholder' => 'Nome do convênio', 'class' => 'form-control']) !!}
                </div>
            </div>

        <div class="row form-group">
            <div class="col-sm-6">
                {!! Form::label('cidade', 'Cidade') !!}
                {{ Form::select('id_cidade', $cidades, null, ['class' => 'form-control', 'id' => 'cidade']) }}
            </div>

            <div class="col-sm-6">
                {!! Form::label('rua', 'Rua') !!}
                {!! Form::input('text', 'rua', null, ['id' => 'rua', 'placeholder' => 'Rua', 'class' => 'form-control']) !!}
            </div>
        </div>

        <div class="row form-group">
            <div class="col-sm-6">
                {!! Form::label('numero', 'Número') !!}
                {!! Form::input('text', 'numero', null, ['id' => 'numero', 'placeholder' => 'Número', 'class' => 'form-control']) !!}
            </div>

            <div class="col-sm-6">
                {!! Form::label('sala', 'Sala') !!}
                {!! Form::input('text', 'sala', null, ['id' => 'sala', 'placeholder' => 'Sala', 'class' => 'form-control']) !!}
            </div>
        </div>

        <div class="row form-group">
            <div class="col-sm-12">
                {!! Form::label('fone', 'Telefone') !!}
            </div>

            <div class="fones">
                <div class="col-sm-3">
                    <input id="fone" type="text" name="fone[]" placeholder="Telefone" class="form-control fone">
                </div>
            </div>

            <a class="btn remover">Remover</a>
            <a class="btn adicionar">Adicionar</a>
        </div>

        <div class="row form-group">
            <div class="col-sm-6">
                {!! Form::label('servico', 'Tipo de serviço') !!}
                {!! Form::select('id_servico', [null => 'Selecione o tipo de serviço'] + $servicos, null, ['class' => 'form-control', 'id' => 'servico']) !!}
            </div>

            <div class="col-sm-6">
                {!! Form::label('especialidades', 'Tipo de especialidade') !!}
                <select name="id_especialidade" id="especialidades" class="form-control">
                    <option value="">Selecione o tipo de serviço</option>
                </select>
            </div>
        </div>

        <div class="row form-group">
            <div class="col-sm-12">
                {!! Form::label('descricao', 'Informações adicionais') !!}
                {!! Form::textarea('descricao', null, ['id' => 'descricao', 'placeholder' => 'Informações adicionais', 'class' => 'form-control']) !!}
            </div>
        </div>

        {!! Form::submit('Salvar', ['class' => 'btn btn-primary']) !!}

        <a href="{{route('convenio.index')}}" class="btn btn-primary">Voltar</a>
    {!! Form::close() !!}
  • have you considered using null ?: $convinio->endereco->rua instead of just null ?

  • I had already thought about it, but wanted some solution q modify only the form and already fill automatic, without having to modify the inputs.

  • There is no way. FMB (Form Model Bind) is based on key/value. Then each input will contain the value corresponding to the key associated with it. In the case street you send to Endereco (I believe another Model). What you can try, I’ve never done this way, is to use 'Convenio.endereco.rua' and etc... in the fields that are related to the Address model.

No answers

Browser other questions tagged

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