1

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

9
  • it is echo Form::radio('name', 'value', true); in laravel's blade template Commented Jan 17, 2017 at 12:24
  • And what about the migration, we don't change anything ? Commented Jan 17, 2017 at 12:28
  • You'll probably need to have a corresponding column in your table. E.g. $table->boolean('important'). Commented Jan 17, 2017 at 13:00
  • @RikardOlsson should I add 1 or 2 columns each choice correspond to a column ? Commented Jan 17, 2017 at 14:43
  • Either so, but more efficient would probably be to do $table->integer('importance') and use maybe a zero to indicate "important" and one to indicate "more important" and so on Commented Jan 17, 2017 at 15:05

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.