将值从下拉列表发送到控制器laravel

时间:2017-04-20 09:19:42

标签: laravel

我是laravel的新手,我需要一个下拉列表,然后选择一个选项,然后单击搜索按钮以显示结果。

控制器:索引显示要选择的所有导师,然后在选择它时,将值传递给select_tutor_page。

public function index()
    {
        $tutors = Tutor::all();
        return view('home', ['tutors' =>$tutors]);
    }
    public function selectTutor(Request $request, $tutorId)
    {
        // $tutorId = Input::get('selectTutor');
        // i think should use input to get the value, but it get error with:Trying to get property of non-object (View: E:\xampp\htdocs\appointment\resources\views\selectTutor.blade.php)

        $tutor = Tutor::find($tutorId);

        return view('selectTutor',['tutor' => $tutor]);

    }

主页视图:

<form action="" method="POST" id="tutors">
{{ csrf_field() }}

<select class="form-control" name="selectTutor" id="selectTutor" data-parsley-required="true">

@foreach($tutors as $tutor)
<option value="{{ $tutor->id }}">{{ $tutor->name }}</option>
@endforeach
</select>

<a href = "{{url('selectTutor/'.$tutor->id)}}" class="btn btn-default" role="button">select</a>
</form>

选择导师页面:

<h1>{{$tutor->name}}<h1>

路线:

Route::get('/home', 'HomeController@index')->name('student.home');
Route::get('selectTutor/{tutorId}', 'HomeController@selectTutor')->name('select.tutor');

请帮忙......

1 个答案:

答案 0 :(得分:0)

这应该适合你:

 $tutorId = $request->selectTutor;

如果您将此行添加到控制器,则可以查看所有当前请求数据:

dd($request->all());

此外,要使其工作,您需要向表单添加操作:

<form action="{{ url('selectTutor/'.$tutor->id) }}" method="POST" id="tutors">

使用提交按钮提交此表单,而不是创建a href链接。