Get selected value in form, seletbox

86 views Asked by At

I have a problem and I can't resolve it, please help me. So I have my form :

{{ Form::open(array('url'=>'/administration/student/addMarks','method' => 'post')) }}
         @foreach($aObjectsInGroupe as $object)
             {{ Form::hidden('id_object[]',$object->id)   }}
             {{ Form::label($object->name) }}
             {{ Form::select('note[]', $aMarks, null, array('class'=>'form-control')) }}
             <br />
         @endforeach
            {{ Form::hidden('id',$aOneStudent['id']) }}
            {{ Form::submit('Add mark',array('class'=>'btn btn-primary')) }}
{{ Form::close() }}

In my StudentController I have a method for get the mark from student_id and object_id:

public function getMarkByStudentAndObject($nIdStudent, $nIdObject){
    $aMark = \Mark::where('student_id', '=', $nIdStudent)
                ->and('object_id', $nIdObject)
                ->get()
                ->toArray();
}

$aMarsks it's a table :

$aMarks = array(
        '0'=>'0',
        '1'=>'1',
        '2'=>'2',
        '3'=>'3',
        '4'=>'4',
        '5'=>'5',
        '6'=>'6',
        '7'=>'7',
        '8'=>'8',
        '9'=>'9',
        '10'=>'10',
    );

It's possible to call the method getMarkByStudentAndObject in :

{{ Form::select('note[]', $aMarks, null, array('class'=>'form-control')) }}

to get the selected value? Help me please. Thx in advance.

3

There are 3 answers

0
Eduardo Pacios On

maybe you need to do this:

<select name="note[]" class="form-control">
    @foreach($aMarks as $key => $value)
        <option value="{{ $key }}">{{ $value }}</option>
    @endforeach
</select>

You should also check your Eloquent query, instead of ->and use another ->where

0
Wader On

Try the lists() function of the Query Builder

\Mark::where('student_id', '=', $nIdStudent)
            ->and('object_id', $nIdObject)
            ->lists('column_1', 'column_2');

You then get the values of column_1 as the keys of the array and column_2 as the values.

http://laravel.com/docs/4.2/queries#selects

0
stengelp On

You should be able to directly call this from the Form::select. A call to

Mark::getMarkByStudentAndObject($aOneStudent['id'], $object->id)->note

would give you the Mark object and then you would need to get the note column which stores the value in the Mark object. Resulting in a call like this:

{{
    Form::select(
        'note[]',
        $aMarks, 
        Mark::getMarkByStudentAndObject(
            $aOneStudent['id'], 
            $object->id
        )->note, array('class'=>'form-control')
    )
}}

At the moment I can't verify if this is working because I have no possibility to test it but it should work as Blade is just a wrapper for PHP calls.