Laravelを勉強している者です。indexページ表示時にユーザーテーブルから取得した結果をtableタグで
表示させているのですが、これを都道府県の条件で抽出した結果を出力させたいです。
実装したソースコードは実行時エラーとなってしまい実現するための方法を探してみましたがなかなか見つかりませんでした。
UserController.php
php
1<?php 2 3namespace App\Http\Controllers; 4 5use Illuminate\Http\Request; 6use Illuminate\Support\Facades\DB; 7 8class UserController extends Controller 9{ 10 //indexページ表示前にユーザーテーブルを取得する 11 public function all() { 12 $users = DB::table('user') 13 ->select('user_id', 'name', 'picture_path', 'prefecture') 14 ->get(); 15 return view('index',[ 16 'db1'=>$users 17 ]); 18 } 19}
index.php
php
1(省略) 2 <head> 3 <meta charset="utf-8"> 4 <meta name="csrf-token" content="{{ csrf_token() }}"> 5 <title>ユーザー一覧</title> 6 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> 7 <link rel="manifest" href="/manifest.json" /> 8 <script> 9 var pref; 10 var infoList; 11 window.onload = function() { 12 //ページ読み込み時にユーザー情報リストを保持しておく 13 infoList = $db1; 14 } 15 16 function selectPref(){ 17 var $elementReference = document.getElementById("kenmei"); 18 var $selectedIndex = $elementReference.selectedIndex; 19 pref = $elementReference.options[$selectedIndex].text; 20 } 21 22 function doSort(){ 23 var sortList = new Array(); 24 for(let i = 0; i < infoList.length; i++) { 25 if(infoList[i].prefecture == pref){ 26 sortList.push(infoList[i]); 27 } 28 } 29 $db1 = sortList; 30 } 31 </script> 32 </head> 33 <body> 34 都道府県 35 <select name="kenmei" id="kenmei" onChange="selectPref()"> 36 <option value="" selected>都道府県</option> 37 <option value="1">北海道</option> 38 <option value="2">青森県</option> 39 (中略) 40 <option value="47">沖縄県</option> 41 </select> 42 <input type="button" value="ソート" onClick="doSort()"> 43 44 <table border="1"> 45 @foreach ($db1 as $user_info) 46 <tr> 47 <td> 48 <img src="{{ $user_info->picture_path }}"> 49 <p>名前:{{ $user_info->name }}</p> 50 <p>出身地: {{ $user_info->prefecture }}</p> 51 </td> 52 <tr> 53 @endforeach 54 </table> 55 </body> 56(省略)
Laravel,phpに詳しい方がいましたら実装例を回答としていただけないでしょうか?
回答2件
あなたの回答
tips
プレビュー