简体   繁体   中英

Laravel search results Not Displaying Properly

I have a Form (Laravel 5.2) for a search. I only have one table and my data displays. If I search by a lastName I would like it to display everyone who has the last name. If I search by manufacturer I would like it to display all PCs made by that manufacturer. I am basically trying to make each column on the database searchable.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Inventory</title>
    <link rel="stylesheet" href="/css/libs.css">
    <link rel="stylesheet" href="/css/app.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  </head>
  <body>

    <nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container-fluid">
  <div class="navbar-header">
    <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
      <span class="sr-only">Toggle navigation</span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
    </button>
    <a class="navbar-brand" href="http://inventory.app:8000/computers/create" Inventory</a>
  </div>
  <div id="navbar" class="navbar-collapse collapse">
    <ul class="nav navbar-nav navbar-right">
      <li>
          {{ Form::open(['method' => 'GET', 'url'=>'computers/search']) }}
          {{ Form::input('search', 'q', null, ['placeholder' => 'Search...']) }}
          {{ Form::close() }}
      </li>
      <li><a href="http://inventory.app:8000/computers/create">Update Inventory</a></li>
      <li><a href="#">Logout</a></li>
    </ul>

  </div>
</div>
</nav>

I am using Route Resource for my Routing. My route for my search is

Route::get('computers/search', 'InventoriesController@search');

My Search Controller is as follows

public function search()

        {
            $search = \Request::get('search'); //<-- we use global request to get the param of URI

//            $search = Input::get('search');

            $inventories = Inventory::where('lastName','LIKE','%'.$search.'%')
               -> orwhere('firstName', 'LIKE','%'.$search.'%' )
                -> orwhere('department', 'LIKE','%'.$search.'%' )
                -> orwhere('building', 'LIKE','%'.$search.'%' )
                -> orwhere('room', 'LIKE','%'.$search.'%' )
                -> orwhere('manufacturer', 'LIKE','%'.$search.'%' )
                -> orwhere('device', 'LIKE','%'.$search.'%' )
                -> orwhere('model', 'LIKE','%'.$search.'%' )
                -> orwhere('tag', 'LIKE','%'.$search.'%' )
                -> orwhere('macAddress', 'LIKE','%'.$search.'%' )
                -> orwhere('status', 'LIKE','%'.$search.'%' )
                -> orwhere('comments', 'LIKE','%'.$search.'%' )
                ->get($search);




            return view('computers.search',compact('inventories'));
        }

I have tried doing an orderBy('search') but that did not do anything. My table just displays but it does redirect to the search route because the URL changes For example http://inventory.app:8000/computers/search?q=Dell

As shown in your URL, the variable you're looking for is q , not search . So get that in your request instead:

$search = \Request::get('q');

Since what you're searching for is most likely not a column, don't use it in your get() call. Just get all columns, or just the ones you want

->get();

or

->get(['lastName','firstName','department',...]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM