Naj
Naj

Reputation: 119

How to use radio button with laravel

I am trying to learn laravel 5, so I strated by creating some simple forms. My form contains (id, title,..) and radio button

<label for="importance">Importance</label>
<input type="radio" name="Importance"
<?php if (isset($importance) && $importance=="tres_important") echo "checked";?>
    value="tres_important">très important
<input type="radio" name="importance"
    <?php if (isset($importance) && $importance=="important") echo "checked";?>
    value="important">important

Now, I need to know what I should add to my migration file, to make it work "create_projets_table"

public function up()
{
    Schema::create('projets', function (Blueprint $table) {
        $table->increments('id',true);
        $table->string('title');
        $table->date('dateDebut');
        $table->timestamps();
    });
}

Thank you in advance

UPDATES :

Based on the comments bellow, I made some changes, but still not working, it seems like something went wrong with "submit.blade.php"

submit.blade.php :

 <div class="form-group"> 
    <label class="radio-inline">
    <input type="radio" id="tres_important" name="importance" value="tres_important">Très important</label>

    <label class="radio-inline">
    <input type="radio" id="important" name="importance" value="important">Important</label>
 </div>

and this is my migration "create_projets_table.php" :

public function up()
{
    Schema::create('projets', function (Blueprint $table) {
        $table->increments('id',true);
        $table->string('title');
        $table->date('dateDebut');
        $table->date('dateFin');
        $table->float('cout');      
        $table->integer('importance');
        $table->timestamps();
    });
 }

This route.php :

Route::post('/submit', function(Request $request) {
$validator = Validator::make($request->all(), [
    'title' => 'required|max:255',
    'annee_realisation' => 'required|max:255',
    'cout' => 'required|max:255',
    'importance' => 'required',
]);
if ($validator->fails()) {
    return back()
        ->withInput()
        ->withErrors($validator);
}

    $projet = new \App\Projet;
    $projet->title = $request->title;
    $projet->annee_realisation = $request->annee_realisation;
    $projet->cout = $request->cout;
    $projet->importance = $request->importance;

    $projet->save();

    return redirect('/');
});

And this my controller "Project_Controller" :

class Project_Controller extends Controller
{
//
$this->validate(request(), [
'importance' => 'required'
]);
}

I got the following error :

Undefined variable : importance

enter image description here

Upvotes: 1

Views: 15758

Answers (2)

ATechGuy
ATechGuy

Reputation: 1270

I don't see anywhere in your code you have set the value of "$importance", can you show the code where you set that value?.

Your checking to see if its set which is fine, but then also checking its value.

if (isset($importance) && $importance=="tres_important"

if it is not set you cant chcek it to see what the value is, which is what it looks like your trying to do in the code i can see. Thats where your undefined error is coming from. If you can post full code it will be easier to track down

Upvotes: 0

Manjeet Barnala
Manjeet Barnala

Reputation: 2995

You can use laravel form methods, like below

Form::radio('name', 'value', true);

Example:

<label class="radio inline">
    {!! Form ::radio('importance','tres_important',($importance == 'tres_important' ? true : false)) !!}
    Très important
</label>
<label class="radio inline">
    {!! Form ::radio('importance','important',($importance == 'important' ? true : false)) !!}
    Important
</label>

Read Documentation https://laravelcollective.com/docs/5.0/html#checkboxes-and-radio-buttons

Upvotes: 3

Related Questions