LoginSignup
0
2

More than 3 years have passed since last update.

Laravel 検索機能サンプル

Posted at

searchアクションで検索結果があれば表示するサンプル

routes.php
Route::get('/person/{id}', 'PersonController@find');
Route::post('/person/{id}', 'PersonController@search');
controller.php

  public function find(Request $request){
    return view('person.find', ['input' => '', 'id' => $request->id]);
  }

  public function search(Request $request){
    $input_val = $request->input;
    $item = Person::find($input_val);
    return view('person.find', ['input' => $input_val, 'id' => $request->id, 'item' => $item]);
  }
view.blade.php
  <form action="/person/{{$id}}" method="post">
    {{ csrf_field() }}
    <input type="text" name="input" value="{{$input}}">
    <input type="submit" value="find">
  </form>

  @if (isset($item))
    <table>
      <tr>
        <th>Data</th>
      </tr>
      <tr>
        <td>{{ $item->getData() }}</td>
      </tr>
    </table>
  @endif
0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2