0

i can not show search result using laravel. below is the code for route, controller and views:

route:

Route::resource('search', 'SearchController');

controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Order;
use Redirect;
use Auth;
use DB;

class SearchController extends Controller
{
    public function index(Request $request)
    {
        $order = Order::all();
        return view(search.index', compact(search));
    }

    public function search(Request $request)
    {
        $search = $request->search;

        $order = DB::table('order')
            ->where('custName','like',"%".$search."%");

        return view('search.index',compact('search'));
    }
}

view

<form action="search" method="GET">
    <input type="text" name="custName" class="form-control" <br>
                <button type="submit" class="btn btn-primary">SEARCH</button>
            </div>
        </div>
        <table class="table table-bordered" width="500">
            <tr>
                <td><font color="white">RESULT :</font></td>
            </tr>
            @foreach($order as $order)
            <tr>
                <td>{{ $order->custName }}</td>
                <td>{{ $order->orderStatus }}</td>
            </tr>
            @endforeach
        </table>

directory location

c:/xampp/htdocs/web/resources/views/search/index.blade.php

show error

Undefined variable: order (View: c:\xampp\htdocs\web\resources\views\search\index.blade.php)

above is my code to search record in the database. but the result always undefined variable.

how to display the searched result using code above. thank you

mogleng
  • 45
  • 3
  • 11
  • pass `$order` to `view`, change this line `return view(search.index', compact(search));` to `return view(search.index', compact('order'));` – Sohel0415 Jun 23 '19 at 11:38

3 Answers3

2

In your Controller search method, assign search result to $orders

$orders = DB::table('order')
            ->where('custName','like',"%".$search."%");

then send it to the view:

 return view('search.index',compact('orders'));

and in the view do this:

@foreach($orders as $order)
   <tr>
      <td>{{ $order->custName }}</td>
      <td>{{ $order->orderStatus }}</td>
   </tr>
 @endforeach

It will solve your problem.

user33192
  • 1,012
  • 1
  • 13
  • 21
  • it show all records, not certain/particular name. what im try to do is. when user type "maxwell" the result show "maxwell" not all records. pls point me how to do it in laravel – mogleng Jun 23 '19 at 14:02
  • @mogleng, What do you mean by 'it show all records, not certain/particular name' and 'show "maxwell" not all records'? Your search query is "%".$search."%" i.e. it would return all those rows which has 'maxwell' in the 'custName' column's value no matter where it is, even in the middle of a word. Ex: maxwell, Mr.maxwell, maxwellmyName – user33192 Jun 24 '19 at 09:14
  • for instance i have a aa aaa b c d mr d – mogleng Jun 25 '19 at 10:14
  • @mogleng, If you still face problem, you can post screenshot of your output – user33192 Jun 26 '19 at 05:13
1

There are some issues with the code you have shared, I try to tell the ones I see.

  1. there is a misspelling in Route::resource where instead of search you coded seearch

  2. as for search method it is not used anywhere by the route you provided. You can see more details on Route::resource for methods and views at this question

  3. now considering that you are loading index.blade.php from the public function index method, the usage of compact is troubled. At this page, you can see how compact and with are different and how to use it. The main problem here is that you are not actually passing the order to your view at all.
  4. The last but not least, it is good practice to name items having a collection of values plurally. For example here the order should change to orders. This would prevent the misuse of it in foreach. In your foreach, the name of the iterating array and the temp variable keeping the data should be different, or otherwise, they would have conflict. Here you have 2 variables called order in the same scope.
jimist
  • 19
  • 3
0

for instance i have records :

  1. a
  2. aa
  3. aaa
  4. b
  5. c
  6. d
  7. e

the code show all record no1 until no7. but now its solved i fixed the code in my controller in index() section as below

 public function index()
 {
        return view('search.index', compact('order'));
 }

 public function search(Request $request)
 {
        $search = $request->search;
        $order = DB::table('order')->where('custName', 'like', "%".$search."%")->get();

        return view('search.result', compact('order'));
    }
mogleng
  • 45
  • 3
  • 11