How to keep a selected radio input in a view in Laravel?

0

How would I keep a input radio selected in my view , I have the following codes:

View:

 <div class="col-md-3"> 
      <div class="form-group">
          <label>Tipo de Conta</label>
          <input type="radio" name="tipo" value="CC"> Conta Corrente
          <input type="radio" name="tipo" value="CP"> Conta Poupança
      </div>
  </div>

Controller:

public function edit($id_conta_bancaria) {
$bancos = Banco::orderBy('nome')->get();
$conta = ContaBancaria::find($id_conta_bancaria);
return view('conta_bancaria.edit')->with(['conta' => $conta, 'bancos' => $bancos]);
}

Model:

namespace App;

use Illuminate\Database\Eloquent\Model;

class ContaBancaria extends Model
{

protected $dates = ['deleted_at'];
protected $table = 'conta_bancaria';
protected $primaryKey = 'id_conta_bancaria';
protected $fillable = ['id_banco', 'agencia', 'conta', 'tipo', 'operacao'];

}

    
asked by anonymous 13.06.2016 / 21:57

2 answers

2

In order for Laravel to redeem the% chosen% use the tipo Request (namespace classe ) , as the following code:

public function index(\Illuminate\Http\Request $request)
{

    if ($request->has('tipo'))
    {

        $tipo = $request->input('tipo');

    }

}
EDITION: The user did not report the problem correctly, he wanted to create his view on his model or set of information by selecting the type that is recorded in your database.

Controller

public function edit($id)
{
    if (isset($id) && is_numeric($id)) 
    {        
        $data['conta'] = $model->find($id);
        return view('editor', $data);
    }
}

View

<div class="col-md-3"> 
    <div class="form-group">
        <label>Tipo de Conta</label>
        <p><input type="radio" name="tipo" value="CC" {{ $conta->tipo == 'CC' ? 'checked' : '' }}> Conta Corrente</p>
        <p><input type="radio" name="tipo" value="CP" {{ $conta->tipo == 'CP' ? 'checked' : '' }}> Conta Poupança</p>
   </div>
</div>
    
14.06.2016 / 02:28
1

I recommend using LaravelCollection / html to generate your forms, problems like this can be easily solved using the Form Model Binding . Since this package is no longer an official dependency of laravel, you should install it via composer.

In addition to making your code more readable, values are automatically assigned to their proper fields according to the attributes of the "template" linked to the form, for example. This also automatically works with values that were explicitly assigned to the session, eliminating the need to use Input::old('field') . Here is a simple example:

{!! Form::model(new App\User(), ['action' => 'Auth\AuthController@postRegister']) !!}

    ...

    <div class="col-md-4">
        <div class="form-group">
            {!! Form::label('email', 'E-mail', ['class' => 'control-label']) !!}
            {!! Form::email('email', null, ['class' => 'form-control']) !!}
        </div>
    </div>

    <div class="col-md-4">
        <div class="radio">
            <label>
                {!! Form::radio('gender', 0, $user->gender == 0) !!}
                Homem
            </label>
            <label>
                {!! Form::radio('gender', 1, $user->gender == 1) !!}
                Mulher
            </label>
        </div>
    </div>

    ...

{!! Form::close() !!}

In this way, the field for the CSRF token is automatically added, and you can assign the method the same way that I assign the class to the form.

{!! Form::model(new App\User(), ['action' => 'Auth\AuthController@postRegister', 'method' => 'post']) !!}
    
17.06.2016 / 15:25