Good day, there is an old project which is written in php / pdo. The task is to transfer to laravel 5.4. Transferred all pages except this one. There is a controller - StatsController and stats.blade.php. How to transfer this code to them?

try{ $dbh = new PDO($dsn,$user,$password); $dbh->exec('SET NAMES utf8'); } catch(PDOException $e){ echo 'Error :'.$e->getMessage(); } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet> </head> <body> <div class="content container"> <table class="table table-hover table-bordered"> <thead> <tr> <th class="text-center">Name</th> <th class="text-center">K</th> <th class="text-center">D</th> <th class="text-center">Points</th> </tr> </thead> <tbody> <? $sql = $dbh->query("SELECT * FROM stats"); $res=$sql->fetch(PDO::FETCH_BOTH); foreach($sql as $res){ $sql2 = $dbh->query("SELECT * FROM skill"); $res2=$sql2->fetch(PDO::FETCH_BOTH); $skill = null; foreach($sql2 as $res2){ if($skill!=null){ continue; } if((int)$res2['max']>=(int)$res['skill']){ $skill=$res2['title']; } } ?> <tr> <td class="text-center"><?=$res['name'];?></td> <td class="text-center"><?=$res['kills'];?></td> <td class="text-center"><?=$res['deaths'];?></td> <td class="text-center"><?=$skill;?></td> </tr> <? } ?> </tbody> </table> </div> </div> </div> </body> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> </html> 

    1 answer 1

    Something like this should look like the code, a bit more optimized it, what to call and how to post is up to you:

    Controller:

     <?php namespace App\Http\Controllers; use Illuminate\Support\Facades\DB; class StatsController extends Controller { public function index() { // $sql = $dbh->query("SELECT * FROM stats"); // $res=$sql->fetch(PDO::FETCH_BOTH); $stats = DB::table('stats')->get(); // $sql2 = $dbh->query("SELECT * FROM skill"); // $res2=$sql2->fetch(PDO::FETCH_BOTH); $skills = DB::table('skill')->get(); return view('stats.index', compact('stats', 'skills')); } } 

    The stats.index view stats.index :

     <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet> </head> <body> <div class="content container"> <table class="table table-hover table-bordered"> <thead> <tr> <th class="text-center">Name</th> <th class="text-center">K</th> <th class="text-center">D</th> <th class="text-center">Points</th> </tr> </thead> <tbody> @foreach($stats as $stat) @include('stats.item') @endforeach </tbody> </table> </div> </div> </div> </body> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> </html> 

    The stats.item view stats.item :

     <tr> <td class="text-center">{{ $stat->name }}</td> <td class="text-center">{{ $stat->kills }}</td> <td class="text-center">{{ $stat->deaths }}</td> <td class="text-center"> @foreach($skills as $skill) @if($skill->max >= $stat->skill) {{ $skill->title }} @break; @endif @endforeach </td> </tr>