title: How to implement search function in Laravel 5.4
body: <p>I'm new to Laravel and have some understanding problems how can I implement a search function to my app.</p>
  
  <p>What I have tried so far.</p>
  
  <p>I created a model that gets the data from the db (this is working)</p>
  
  <pre><code>class Post extends Model
  {
      public static function scopeSearch($query, $searchTerm)
      {
          return $query-&gt;where('city', 'like', '%' .$searchTerm. '%')
                       -&gt;orWhere('name', 'like', '%' .$searchTerm. '%');
      }
  }
  </code></pre>
  
  <p>My controller looks like this:</p>
  
  <pre><code>use App\Post;
  
      class PostsController extends Controller
      {
          public function index(Request $request) 
          {
              $searchTerm = $request-&gt;input('searchTerm');
              $posts = Post::all()
              -&gt;search($searchTerm);
              return view('posts.index', compact('posts', 'searchTerm'));
          }
  
         public function show($id)
         {
             $post = Post::find($id);
             return view('posts.show', compact('post'));
         }
      }
  </code></pre>
  
  <p>With php artisan tinker the model is working. I'm getting the db query.</p>
  
  <p>On the front-end I have a simple input field with a submit button:</p>
  
  <pre><code>&lt;div class="container"&gt;
      &lt;div class="col-sm-8"&gt;
          &lt;form action="posts.index" method="GET"&gt;
              {{csrf_field()}}
              &lt;div class="input-group"&gt;
                  &lt;input type="text" class="form-control" name="searchTerm" placeholder="Search for..." value="{{ isset($searchTerm) ? $searchTerm : '' }}"&gt;
                  &lt;span class="input-group-btn"&gt;
                      &lt;button class="btn btn-secondary" type="submit"&gt;Search&lt;/button&gt;
                  &lt;/span&gt;
              &lt;/div&gt;
          &lt;/form&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>But, now, how can I retrieve the data from the controller if I input something in the search input field? The code is only working when I get all the posts. I need to include somehow the <code>serachTerm</code> from the controller</p>
  
  <pre><code>@extends('layouts.master')
  
  @section('content')
  &lt;div class="col-sm-8 blog-main"&gt;
      @foreach($posts as $post)
          &lt;div class="card" style="margin-bottom: 10px"&gt;
              &lt;div class="card-block"&gt;
                  &lt;h3 class="card-title"&gt;{{ $post-&gt;Name1 }}&lt;/h3&gt;
                  &lt;small&gt;Created at: {{ $post-&gt;created_at-&gt;toFormattedDateString() }}&lt;/small&gt;
                  &lt;p class="card-text"&gt;With supporting text below as a natural lead-in to additional content.&lt;/p&gt;
                  &lt;a href="{{$post-&gt;id}}" class="btn btn-primary"&gt;Details&lt;/a&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      @endforeach
  
  &lt;/div&gt;
  @endsection
  </code></pre>