検索をした時に検索BOXに検索したキーワードをそのまま残しておきたいと思い試行錯誤しております。
value="{{$keyword}}"
でするとUndefined variable: keyword のエラーが
value="{{ old('keyword', $customers->keyword) }}
でするとUndefined property:Illuminate\Pagination\LengthAwarePaginator::$keyword と言うエラーが
@if (isset( $keyword )) {{$keyword}} @endif
で、するとそもそも$keywordが無いのか?そのままエラーも出ずに表示されず
変数の受け取り方が解ってないのが原因かもしれませんが
自分で試せることはやり尽くしてしまったのでご教示頂けると幸いです。
index.blade.php
php
1@section('content') 2<h1>@yield('title')</h1> 3<div class="search"> 4 <!--↓↓ 検索フォーム ↓↓--> 5<div class="col-sm-4" style="padding:20px 0; padding-left:0px;"> 6<form class="form-inline" action="{{url('/search')}}"> 7 <div class="form-group"> 8 <input type="text" name="keyword" value="@if (isset( $keyword )) $keyword @endif" class="form-control" placeholder="名前を入力してください"> 9 </div> 10 <input type="submit" value="検索" class="btn btn-info"> 11</form> 12</div> 13<!--↑↑ 検索フォーム ↑↑--> 14</div> 15 16 <table class="table index type01"> 17 <thead> 18 <tr> 19 <th>ID</th> 20 <th>名前</th> 21 <th>ナマエ</th> 22 <th>電話番号</th> 23 <th>携帯番号</th> 24 <th>メモ</th> 25 <th>処理</th> 26 </tr> 27</thead> 28@foreach($customers as $customer) 29 <tr> 30 <td>{{$customer->id}}</td> 31 <td>{{$customer->name1}}</td> 32 <td>{{$customer->name2}}</td> 33 <td>{{$customer->tel}}</td> 34 <td>{{$customer->mobile}}</td> 35 {{--nl2brで改行を表示--}} 36 <td>{!! nl2br(e($customer->memo)) !!}</td> 37 <td> 38 <a href="{{url('/show',$customer->id)}}"><span class="btn"></span>詳細</a> 39 <a href="{{url('/edit',$customer->id)}}"><span class="btn"></span>修正</a> 40 <a href="#" data-id='{{$customer->id}}' class="del"><span class="btn"></span>削除</a> 41 <form action="{{url('/delete',$customer->id)}}" method="post" id='form_{{$customer->id}}'> 42 {{ csrf_field() }} 43 {{ method_field('delete') }} 44 </form> 45 </td> 46 47</tr> 48@endforeach 49</table> 50<script type="text/javascript" src="/js/main.js"></script> 51{{ $customers->links() }} 52@endsection
CustomersController.php
php
1use Illuminate\Http\Request; 2use App\Customer; 3 4class CustomersController extends BaseController 5{ 6 use AuthorizesRequests, DispatchesJobs, ValidatesRequests; 7 8 public function index(){ 9 //$customers =Customer::all(); 10 $customers =Customer::paginate(3); 11 //dd($customers->toArray()); // dump die 12 return view('customer.index')->with('customers',$customers); 13 } 14//検索 15public function search(Request $request) 16{ 17 #キーワード受け取り 18 $keyword = $request->input('keyword'); 19 20 #クエリ生成 21 $query = Customer::query(); 22 23 #もしキーワードがあったら 24 if(!empty($keyword)) 25 { 26 $query->where('name1','like','%'.$keyword.'%') 27 ->orWhere('name2','like','%'.$keyword.'%') 28 ->orWhere('tel','like','%'.$keyword.'%') 29 ->orWhere('mobile','like','%'.$keyword.'%'); 30 } 31 $customers = $query->orderBy('created_at','desc')->paginate(10); 32 return view('customer.index')->with('customers',$customers); 33 34} 35}
web.php
php
1Route::get('/', 'CustomersController@index'); 2Route::get('/search/', 'CustomersController@search');
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/11/25 08:00