mogleng
mogleng

Reputation: 55

how to display search result in laravel

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

Upvotes: 0

Views: 5399

Answers (3)

mogleng
mogleng

Reputation: 55

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'));
    }

Upvotes: 0

user33192
user33192

Reputation: 1182

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.

Upvotes: 2

jimist
jimist

Reputation: 19

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.

Upvotes: 1

Related Questions