前提
Userテーブル:全校生徒のデータ
そこから、自分のクラスの生徒のデータからだけで検索できるように実装したい
実現したいこと
一般的な検索コードで作成しましたが、検索結果が全く表示されません。(同様の書きかたで、テーブル全体から検索する場合には、問題なく検索出来ている。)
様々な検索パターンを試してみたものの、成功には至りません。どなたかご助言いただけないでしょうか?
宜しくお願い致します。
該当のソースコード
コントローラー画面
php
1/*個別データ検索へ*/ 2 public function individual(Request $request,$id) 3 { 4 /*入力ワード*/ 5 $searchWord = $request->input('searchWord'); 6 /*検索対象データ*/ 7 $query = User::where('school1', '=', $id)->orWhere('school2', '=', $id); 8 //名前が入力された場合、ユーザーテーブルから一致する名前を$queryに代入 9 if(!empty($searchWord)) { 10 $query->where('name', 'LIKE', "%{$searchWord}%"); 11 } 12 //$queryをtuser_idの昇順に並び替えて$usersに代入 13 $users = $query->orderBy('id', 'desc')->paginate(10); 14 15 16 return view('individual', [ 17 'users' => $users, 18 'searchWord' => $searchWord, 19 ]); 20 }
view画面
php
1<div class="searchtable-responsive" > 2 <div class="test"> 3 <h2 class="text-center">ユーザー検索画面</h2> 4 <!--検索フォーム--> 5 <div class="row"> 6 <div class="col-sm"> 7 <form method="GET" action="{{ route('individual',['id'])}}"> 8 <div class="form-group row"> 9 <label class="col-sm-2 col-form-label" >ユーザー名</label> 10 <!--入力--> 11 <div class="col-sm-5"> 12 <input type="text" name="searchWord" placeholder="検索したい生徒名を入力してください" value="{{ $searchWord }}"> 13 </div> 14 <div class="col-sm-auto"> 15 <button type="submit" class="btn btn-primary ">生徒検索</button> 16 </div> 17 </div> 18 </form> 19 </div> 20 </div> 21 </div> 22 23<!--検索結果テーブル 検索された時のみ表示する--> 24 @if (!empty($users)) 25 <div class="test-hover"> 26 <p>全{{ $users->count() }}件</p> 27 <table class="table table-hover"> 28 <thead style="background-color: #ffd900"> 29 <tr> 30 <th style="width:20%">生徒名</th> 31 <th style="width:10%">学年</th> 32 <th style="width:20%">テスト履歴表示へ</th> 33 </tr> 34 </thead> 35 @foreach($users as $user) 36 <tr> 37 <td>{{ $user->name }}</td> 38 <td>{{$user->year }}</td> 39 <td ><div class="button"><a href="{{ route('id_view',['id'=>$user->id]) }}">表示</a></div></td> 40 </tr> 41 @endforeach 42 </table> 43 </div> 44 <!--テーブルここまで--> 45 <!--ページネーション--> 46 <div class="d-flex justify-content-center"> 47 {{-- appendsでカテゴリを選択したまま遷移 --}} 48 {{ $users->appends(request()->input())->links() }} 49 </div> 50 <!--ページネーションここまで--> 51 @endif 52 </div> 53 </div> 54</main> 55@endsection
サンプルデータ
php
1 $users =[ 2 ['name' => '山田太朗', 3 'user_name' => 'yamada', 4 'school1' => '11', 5 'school2' => '000', 6 'place' => '沖縄県', 7 'year' => '中2', 8 'point' => '0', 9 'email' => 'taro@example.com', 10 'password' => 'password'] 11 ];
create_users_table.php
php
1 Schema::create('users', function (Blueprint $table) { 2 $table->id(); 3 $table->string('name'); 4 $table->string('user_name')->unique(); 5 $table->string('school1')->nullable()->default('000')->index(); 6 $table->string('school2')->nullable()->default('000')->index(); 7 $table->string('email')->unique(); 8 $table->timestamp('email_verified_at')->nullable(); 9 $table->string('password'); 10 $table->string('image')->nullable(); 11 $table->string('place'); 12 $table->string('year'); 13 $table->integer('point')->default(0); 14 $table->integer('level')->default(0); 15 $table->rememberToken(); 16 $table->timestamps(); 17 });
**web.php
**
php
1/*個別データ検索*/ 2Route::get('/individual/{id}',[App\Http\Controllers\TestController::class,'individual'])->name('individual');
試したこと
コントローラー内
$query = User::where('school1', '=', $id)->orWhere('school2', '=', $id);を
$query = User::query();に
$query->where('name', 'LIKE', "%{$searchWord}%");を
$query->where('school1', '=', $id)->orWhere('school2', '=', $id)where('name', 'LIKE', "%{$searchWord}%");に変更
同様の結果でした。

回答1件
あなたの回答
tips
プレビュー