2
votes

I am new to laravel, I need a dropdown list, then select one option, and click search button to show the result.

Controller: index shows all tutors to be select, then when select it, pass the value to the 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]);

    }

Home page view:

<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>

Select tutor page:

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

Route:

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

please help....

1

1 Answers

0
votes

This should work for you:

 $tutorId = $request->selectTutor;

You can see all current request data if you add this line to your controller:

dd($request->all());

Also, to make it work, you'll need to add an action to the form:

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

And submit this form with submit button instead of creating a href link.