-1

I try this tutorial on laravel about Live Search

But it's on the homepage(index)

I want to access it to localhost/laravel/public/search

Here is the Controller

class SearchController extends Controller
{
public function index()
{
return view('search.search');
}

public function search(Request $request)
{
   if ($request->ajax())
       $output ="";
       $orderinfo=DB::table('tb_order')->where('shipcustomername','LIKE','%' . $request->search.'%' )
                                       ->orWhere('orderId','LIKE','%' .$request->search. '%')->get();

   if ($orderinfo)
   {

       foreach ($orderinfo as $key =>$orderinfo ){


       $output.='<tr>' . 
               '<td>' .$orderinfo->orderId  .'</td>' .
               '<td>' .$orderinfo->email  .'</td>' .
               '<td>' .$orderinfo->subsource  .'</td>' .

               '</tr>';   

       }

       return Response($output);

   }

and my route

Route::get('/' ,'SearchController@index');
Route::get('/search' ,'SearchController@search');

on my resources folder i have folder search and it's contain the search.blade.php

 <div class="container">
        <div class="row">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3>Order Info</h3>               
            </div>
            <div class="panel-body">
                <div class="form-group">
                    <input type="text" class="form-control" id="search" name="search"></input>                 
                </div>
                <table class="table table-bordered table-hover ">
                    <thead>
                        <tr>
                            <th>OrderID</th>
                            <th>Email</th>
                            <th>SubSource</th>
                        </tr>
                    </thead>
                    <tbody>

                    </tbody>
                   </table>       
                </div>
        </div>
    </div>
        <script type="text/javascript">
            $('#search').on('keyup',function(){
               $value=$(this).val();
               $.ajax({

                  type : 'get',
                  url  : '{{URL::to('search')}}',
                  data : {'search':$value},
                  success:function(data){
                      $('tbody').html(data);                   
                  }

               });
            });
        </script>

</body>

I know this is the route for index ,

 Route::get('/' ,'SearchController@index');

But if try to change this to

 Route::get('search' ,'SearchController@index');

I get error 500

What is the correct way to route this so it will not use the index

Thank you

roy phil
  • 17
  • 1
  • 6
  • Try changing `Route::get('search' ,'SearchController@index');` to `Route::get('/search' ,'SearchController@index');` – M_Idrees Aug 14 '17 at 19:57
  • please add the search blade as well – Leo Aug 14 '17 at 20:21
  • @Leo_Kelmendi I have search.blade.php on view folder too – roy phil Aug 14 '17 at 20:23
  • Add it to the question I want to see if you have the csrf token set – Leo Aug 14 '17 at 20:23
  • set APP_DEBUG in .env to true since the request is ajax, using chrome and press f12, go to Network tab -> click on error -> preview tab, if it just say error with a blank screen, then maybe you should chmod 775(write permissions) and try again. – Leo Aug 14 '17 at 20:27
  • @Leo_Kelmendi i already add my search.blade.php – roy phil Aug 14 '17 at 20:29

1 Answers1

1

There is a good chance that you are sending empty data try to change this:

$value=$(this).val();

to this:

var value = $('#search').val();

If no that then also you are not submitting the data as well add the form:

{{ Form::open(array('method'=>'GET','class'=> 'col-md-6','url' => '/search', 'id'=>'searchform')) }}

<div class="form-group">
   <input type="text" class="form-control" id="search" name="search"></input>                 
</div>

  {{ Form::close() }}

change your ajax request to this:

 $('#searchform').on('submit', function(e) {
       e.preventDefault(); 
       var search = $('#search').val();
       $.ajax({
           type: "GET",
           url: {{URL::to('search')}},
           data: {search:search}
           success:function(data){
                      $('tbody').html(data);                   
                  }
       });
   });

If not that then:

set APP_DEBUG in .env to true since the request is ajax, using chrome and press f12, go to Network tab -> click on error -> preview tab, if it just say error with a blank screen, then maybe you should chmod 775(write permissions) and try again

Leo
  • 7,274
  • 5
  • 26
  • 48
  • Thank you for suggestion , now i solve my problem , i have to access it by localhost/laravel/public /search/search – roy phil Aug 14 '17 at 22:52