2

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

Xiaozhou Song
  • 161
  • 2
  • 4
  • 14

1 Answers1

0

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.

Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • dd($request->all()); what is mean? and where i should add this? in index()? or selectTutor()? – Xiaozhou Song Apr 20 '17 at 09:23
  • @XiaozhouSong `dd()` is Laravel helper which will display data dump and stop the app execution. `$request->all()` is an array with all current request data. You should put this to the `selectTutor` method. – Alexey Mezenin Apr 20 '17 at 09:25
  • I add the dd($request->all()); to the selectTutor(), and it only show [] ..with ..
    []
    
    – Xiaozhou Song Apr 20 '17 at 09:29
  • @XiaozhouSong that's because you're not submitting the form, please read an updated answer. – Alexey Mezenin Apr 20 '17 at 09:39
  • I know i shouldn't use link but when i use
    ..... i got error with Undefined variable: tutor ....
    – Xiaozhou Song Apr 20 '17 at 10:00
  • @XiaozhouSong you may want to read about routes. Looks like here you don't want to pass an ID at all, so you should remove it from the route. – Alexey Mezenin Apr 20 '17 at 10:01
  • Missing argument 2 for App\Http\Controllers\HomeController::selectTutor()....if i remove the $tutor->id from view and route... – Xiaozhou Song Apr 20 '17 at 10:18
  • and I get url with : http://localhost/appointment/public/selectTutor?_token=6tlodVrGPYBg5q1bJaxah2798dwN789YcVTp6z2N&selectTutor=2&submitBtn=select – Xiaozhou Song Apr 20 '17 at 10:21
  • @XiaozhouSong your web server should be pointed [to the `public` directory](http://stackoverflow.com/questions/37507209/how-to-hide-config-files-from-direct-access). And if you're trying to use the route without ID, it should be `'selectTutor'` instead of `'selectTutor/{tutorId}'` and it should also be `post`, not `get` – Alexey Mezenin Apr 20 '17 at 12:37