Saturday, April 1, 2017

Why I get [Laravel - Undefined variable: users (View..] if I tried to use POST and GET on the same view?

I am trying to make a simple search form using laravel. the search page is supposed to be the same for results. but I can't make it work, and i'm not sure why It works on a different Route but can't get it to work on the same route.

Routes:

Route::get('searchuser',function(){
return view('searchuser');

 })->middleware('auth');


 Route::post('searchuser', function(){
 $q = Input::get('name');
 $users2 = App\User::where('name_en','like',$q)->get();

 return view('searchuser', ['users' => $users]);

 });

Blade:

                <div class="panel-body">
                    <form class="form-horizontal" role="form" method="POST" action="searchuser">
                    

                    <div class="form-group">
                        <label for="name" class="col-md-4 control-label">Name</label>

                        <div class="col-md-6">
                            <input id="name" type="text" class="form-control" name="name" value="" placeholder="Search.." required autofocus>

                            @if ($errors->has('name'))
                                <span class="help-block">
                                    <strong></strong>
                                </span>
                            @endif
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-md-6 col-md-offset-4">
                            <button type="submit" class="btn btn-primary">
                                Search
                            </button>

                        </div>
                    </div>
                </form>
                <div>
                    @foreach($users as $user)
                        
                    @endforeach
                </div>
            </div>



via medo ampir

Advertisement