Making a radio button checked in laravel collectives - laravel

How to make a radio button checked in laravel collectives?
{{Form::radio('gender','1',['class'=>'form-check-input'])}}
I'm taking the value from the database and I need to select the appropriate gender value.
$data->gender
Full code is as follows
<div class="col-sm-9" style="margin:auto">
<div class="form-check form-check-inline">
{{Form::radio('gender','1',['class'=>'form-check-input'])}}
<label class="form-check-label ml-2" for="inlineRadio1">Male</label>
</div>
<div class="form-check form-check-inline">
{{Form::radio('gender','2',['class'=>'form-check-input'])}}
<label class="form-check-label ml-2" for="inlineRadio2">Female</label>
</div>
</div>

The data to populate your form should go on your Form::model() method.
Something like this:
On the controller function
// on the controller function
$data['gender'] = 1;
return view('my-view')
->with('data' $data);
On the template
{{ Form::model($data/*, ...*/) }}
<div class="col-sm-9" style="margin:auto">
<div class="form-check form-check-inline">
{{Form::radio('gender','1',['class'=>'form-check-input'])}}
<label class="form-check-label ml-2" for="inlineRadio1">Male</label>
</div>
<div class="form-check form-check-inline">
{{Form::radio('gender','2',['class'=>'form-check-input'])}}
<label class="form-check-label ml-2" for="inlineRadio2">Female</label>
</div>
</div>
{{ Form::close() }}

Add true before options array and empty '' on others
{{ Form::radio('gender', 1, true, ['class'=>'form-check-input', 'id' => 'inlineRadio1']) }}
{{ Form::label('inlineRadio1', 'Male', ['class' => 'form-check-label']) }}
{{ Form::radio('gender', 2, '', ['class'=>'form-check-input', 'id' => 'inlineRadio2']) }}
{{ Form::label('inlineRadio2', 'Female', ['class' => 'form-check-label']) }}

Related

Form Switch from Eloquent

I'm using a Creative Tim template for my first Laravel project: Argon Pro 2 for Laravel, I can't get the form-switch field formatting to work for me using eloquent for my Crud. No problem with HTML. Any ideas?
With HTML (Blade):
<div class="form-check form-switch">
<input id="repetition" name="repetition" class="form-check-input" type="checkbox" id="CheckRepetition" value="{{ old('repetition') }}">
<label class="form-check-label" for="CheckRepetition">Activa para RepeticiĆ³n</label>
</div>
With Eloquent:
<div class="col-3 mb-3">
{{ Form::label('Tarea Repetitiva Checkbox *') }}
{{ Form::checkbox('repetition', $task->repetition, ['class' => 'form-switch ' .($errors->has('repetition') ? ' is-invalid' : '')]) }}
{!! $errors->first('repetition', '<div class="invalid-feedback">:message</div>') !!}
</div>
Thanks in advance ;)
Solved!
My code:
<div class="form-group">
<div class="col-2 mb-3">
{{ Form::label('Tarea Repetitiva') }}
{{ Form::hidden('repetition', '0') }}
<div class="form-check form-switch">
{{ Form::checkbox('repetition', '1', $task->repetition, ['class' => 'form-check-input' . ($errors->has('repetition') ? ' is-invalid' : '')]) }}
</div>
{!! $errors->first('repetition', '<div class="invalid-feedback">:message</div>') !!}
</div>
</div>

Laravel save related data in database

I want to save question_id in answer table using post form
I tried to use foreach or functions but i always got null data
Controller
public function store(Request $request, Survey $survey)
{
$request->validate([
'answer' => 'required',
]);
$survey->option_name = unserialize($survey->option_name);
$answers = new Answer([
'answer' => $request->get('answer'),
'commentaire' => $request->get('commentaire'),
'user_id' => auth()->id(),
'last_ip' => request()->ip(),
'question_id' => $survey->questions,
'survey_id' => $survey->id,
]);
$answers->save();
return redirect()->action('SurveyController#view_survey_answers', [$survey->id]);
}
answers table
question table's row :
id
survey_id
title
timestamp
I got always null data or i tried using where but i got errors like id index doesn't exists...
View :
{!! Form::open(array('action'=>array('AnswerController#store', $survey->id))) !!}
#forelse ($survey->questions as $key=>$question)
<p class="flow-text">Question {{ $key+1 }} - {{ $question->title }}</p>
#if($question->question_type === 'text')
<div class="form-group">
<div class="input-field col s12">
<input id="answer" type="text" name="answer">
<label for="answer">Answer</label>
</div>
</div>
#elseif($question->question_type === 'textarea')
<div class="form-group">
<div class="input-field col s12">
<textarea id="textarea1" class="materialize-textarea" name="{{ $question->id }}[answer]"></textarea>
<label for="textarea1">Textarea</label>
</div>
</div>
#elseif($question->question_type === 'radio')
#foreach($question->option_name as $key=>$value)
<p style="margin:0px; padding:0px;">
#if($value === 'else')
<div class="form-group" style="margin-left: 20px;">
<input name="answer" class="custom-control-input" type="radio" id="{{ $value }}" value="{{$value}}"/>
<label class="custom-control-label" for="{{ $value }}">{{ $value }}</label>
<div id="textboxes" style="display: none">
<br>
<textarea class="form-control" name="commentaire" id="exampleFormControlTextarea1" rows="3" placeholder="Write a large text here ..."></textarea>
</div>
</div>
#else
<p style="margin:0px; padding:0px;">
<div class="form-group" style="margin-left: 20px;">
<input name="answer" class="custom-control-input" type="radio" id="{{ $value }}" value="{{ $value}}"/>
<label class="custom-control-label" for="{{ $value }}">{{ $value }}</label>
</div>
</p>
#endif
#endforeach
#elseif($question->question_type === 'checkbox')
#foreach($question->option_name as $key=>$value)
<p style="margin:0px; padding:0px;">
<div class="form-group">
<input type="checkbox" id="{{ $value }}" name="answer" value="{{$value}}"/>
<label for="{{$value}}">{{ $value }}</label>
</div>
</p>
#endforeach
#endif
<div class="divider" style="margin:10px 10px;"></div>
#empty
<span class='flow-text center-align'>Nothing to show</span>
#endempty
<div class="form-group">
{{ Form::submit('Submit Survey', array('class'=>'btn btn-success mt-4')) }}
</div>
{!! Form::close() !!}

Laravel required if validation issue

Laravel required if validation issue
Blade:
{{ Form::open(['route' => ['updateEmailSettings']]) }}
<div class="form-group row">
{{ Form::label('driver','Mail Driver',['class' => 'col-md-3 col-form-label required']) }}
<div class="col-md-9">
{{ Form::select('driver',$drivers, null,['class' => 'form-control', 'placeholder' => 'Select']) }}
</div>
</div>
<div class="form-group row">
<label class="col-md-3 col-form-label font-weight-bold">Mandrill</label>
</div>
<div class="form-group row">
{{ Form::label('mailgun_secret','Secret',['class' => 'col-md-3 col-form-label']) }}
<div class="col-md-9">
{{ Form::text('mailgun["secret"]',null,['class' => 'form-control', 'id' => 'mailgun_secret']) }}
</div>
</div>
<div class="form-group row">
<div class="col-md-9 ml-md-auto">
{{ Form::button('<i class="far fa-save"></i> Save',['class'=>'btn btn-primary mr-3','type'=>'submit']) }}
<a class="btn btn-danger" href="{{ route('emailSettings') }}"><i class="far fa-times-circle"></i> Cancel</a>
</div>
</div>
{{ Form::close() }}
Form Request:
return [
'driver' => 'required',
'mailgun.*.domain' => 'required_if:driver,mailgun'
];
Validation always fails. Please suggest me if i miss anything.
Resolved myself
Blade: Removed double quotes inside the bracket.
{{ Form::text('mailgun[secret]',null,['class' => 'form-control', 'id' => 'mailgun_secret']) }}
Form Request: Removed asterisk
'mailgun.domain' => 'required_if:driver,mailgun'

Link Button Not Working in Laravel

I want to edit a record in a CRUD application in laravel where I have a button which has been linked to go to the index view but when I click it, it redirects me to the UPDATE method of the controller.
This is my form:
{!! Form::open(['route' => ['players.update', $player->id], 'method' => 'PUT', 'files'=>'true']) !!}
<div class="row col-md-10 col-md-offset-1 panel">
<div class="col-md-8 col-md-offset-2">
<br />
<div class="form-group">
{{ Form::label('name', 'Player Name') }}
{{ Form::text('name', $player->name, ['class' => 'form-control']) }}
</div>
<div class="form-group">
{{ Form::label('file', 'Upload Image') }}
{{ Form::file('pic') }}
</div>
<div class="form-group">
{{Form::button('Save Record', ['type' => 'submit', 'class' => 'btn btn-success'])}}
{!! Form::close() !!}
<a href="{{ route('players.index') }}">
<button class="btn btn-danger" >Cancel</button>
</a>
</div>
</div>
</div>
I have the following button for going back to the index page but this is taking me to the UPDATE method of the controller:
<a href="{{ route('players.index') }}">
<button class="btn btn-danger" >Cancel</button>
</a>
This is my index method in the controller:
public function index()
{
$players = Player::paginate(5);
return view('players.index', compact('players'));
}
This is the UPDATE method in the controller:
public function update(Request $request, $id)
{
return "Hi";
}
This is my route file contents:
Route::resource('news', 'NewsController');
Route::resource('competition', 'CompetitionsController');
Route::resource('players', 'PlayersConroller');
Everything looks fine to me but I don't know what goes wrong here.
Any help is appreciated in advance.
I am not sure if it will solve your issue, try to put your button code outside the form-group div.
You can change your code as
Cancel
You can check your html you have put button inside form tag which of type submit that's why it is submitting the form again.
Replace your form code with:
<div class="row col-md-10 col-md-offset-1 panel">
<div class="col-md-8 col-md-offset-2">
{!! Form::open(['route' => ['players.update', $player->id], 'method' => 'PUT', 'files'=>'true']) !!}
<br />
<div class="form-group">
{{ Form::label('name', 'Player Name') }} {{ Form::text('name', $player->name, ['class' => 'form-control']) }}
</div>
<div class="form-group">
{{ Form::label('file', 'Upload Image') }} {{ Form::file('pic') }}
</div>
<div class="form-group">
{{Form::button('Save Record', ['type' => 'submit', 'class' => 'btn btn-success'])}}
</div>
{!! Form::close() !!}
</div>
<a href="{{ route('players.index') }}">
<button class="btn btn-danger">Cancel</button>
</a>
</div>

create a Select Box form From Database in Laravel

I have 2 select box in this form, the first select box is done and works.
...But when I add the second select box, the error appears like the picture..
here's the code
This is the first select box - it works:
{{ Form::open(array('url' =>route('units.store'), 'method' => 'post' ))}}
<div class="form-group #if ($errors->has('brand_id')) has-error #endif">
<label>Brand Manufacture</label>
{{Form::select('brand_id', $brand, null, ['class' => 'form-control w450']) }}
#if ($errors->has('brand_id')) <p class="help-block">{{ $errors->first('brand_id') }}</p> #endif
</div>
And this is the select that is not right:
<div class="form-group #if ($errors->has('model_id')) has-error #endif">
<label>Model Type</label>
{{Form::select('model_id',$model, null, ['class' => 'form-control w450']) }}
#if ($errors->has('model_id')) <p class="help-block">{{ $errors->first('model_id') }}</p> #endif
</div>
And this is the whole code.
{{ Form::open(array('url' =>route('units.store'), 'method' => 'post' ))}}
<div class="form-group #if ($errors->has('brand_id')) has-error #endif">
<label>Brand Manufacture</label>
{{Form::select('brand_id', $brand, null, ['class' => 'form-control w450']) }}
#if ($errors->has('brand_id')) <p class="help-block">{{ $errors->first('brand_id') }}</p> #endif
</div>
<div class="form-group #if ($errors->has('model_id')) has-error #endif">
<label>Model Type</label>
{{Form::select('model_id',$model, null, ['class' => 'form-control w450']) }}
#if ($errors->has('model_id')) <p class="help-block">{{ $errors->first('model_id') }}</p> #endif
</div>
<div class="form-group #if ($errors->has('kva')) has-error #endif">
<label>kva</label>
<input name="kva" type="text" class="form-control w450" value="{{ Input::old('kva') }}">
#if ($errors->has('kva')) <p class="help-block">{{ $errors->first('kva') }}</p> #endif
</div>
<div class="form-group #if ($errors->has('type')) has-error #endif">
<label>Type</label>
<select name="type" id="" class="form-control w450">
<option value="OPEN">OPEN</option>
<option value="CLOSE">CLOSE</option>
</select>
#if ($errors->has('type')) <p class="help-block">{{ $errors->first('type') }}</p> #endif
</div>
<div class="form-group #if ($errors->has('sn')) has-error #endif">
<label>Serial Number</label>
<input name="sn" type="text" class="form-control w450" value="{{ Input::old('sn') }}">
#if ($errors->has('sn')) <p class="help-block">{{ $errors->first('sn') }}</p> #endif
</div>
<div class="form-group #if ($errors->has('wbs_unit')) has-error #endif">
<label>WBS unit</label>
<input name="wbs_unit" type="text" class="form-control w450" value="{{ Input::old('wbs_unit') }}">
#if ($errors->has('wbs_unit')) <p class="help-block">{{ $errors->first('wbs_unit') }}</p> #endif
</div>
<div class="form-group">
<button type="submit" class="btn btn-sm btn-primary">create</button>
</div>
{{ Form::close() }}
I can't post any image :(
so help me..please
Make sure you're pulling data from the database in the correct format for the select box. It should be an associative array with the key being the database ID and the value being the string you want to show in the dropdown. Here's an example.
$brands = Brand::lists('name', 'id');
// Example: ['1' => 'Adidas', '2' => 'Nike', '3' => 'New Balance;];
{{ Form::select('brand_id', $brands) }}
Also, you can simplify your error class using a ternary statement, something like this.
<div class="form-group {{ $errors->has('brand_id') ? 'has-error' : null }}">

Resources