By GokiSoft.com|
18:29 30/10/2021|
Học Laravel
[Examination] Examination & Test + Website tra cứu luật giao thông đường bộ - Lập trình PHP - Lập trình Laravel
THỜI GIAN THI : 13h50 - 15h00
Tạo bảng đặt tên : luatgiaothong gồm các trường > id tự tăng, title, nội dung, giá tiền phạt min, giá tiền phạt max, ngày có hiệu lực
Yêu cầu:
- Fake 50 bản ghi dữ liệu cho bảng trên
- Viết trang tra cứu thông tin => thực hiện phân trang 10 bản ghi/1 trang => có input nhập tìm kiếm => Khi người dùng gõ vào tìm kiếm theo tiêu đề (sử dụng like trong tìm kiếm).
- Click vào từng tin thì hiển thị trang chi tiết nội dung.
=== HẾT ===
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)
![Nguyễn Tiến Đạt [T2008A]](https://www.gravatar.com/avatar/b5819cd0adc95c727c7ad0c2bcf6098b.jpg?s=80&d=mm&r=g)
Nguyễn Tiến Đạt
2021-06-29 13:44:29
<?php
namespace App\Http\Controllers\Traffic;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class TrafficController extends Controller
{
public function showTraffic(Request $request)
{
$search = $request -> search;
$trafficList = DB::table('traffic');
if(isset($search)){
$trafficList = $trafficList->where('traffic.title','like','%'.$search.'%');
}
$trafficList = $trafficList->paginate(5);
$trafficList->appends(['search' => $search]);
return view('traffic.index')->with([
'trafficList' => $trafficList,
'index' => 1,
'search' => $search
]);
}
}
![Trương Công Vinh [T1907A]](https://www.gravatar.com/avatar/223a7e3a46f4a747f81b921fe023fcc4.jpg?s=80&d=mm&r=g)
Trương Công Vinh
2020-07-17 07:56:26
luatgiaothong.php
<?php
Route::group(['prefix' => 'luatgiaothong'], function () {
Route::get('/list','luatgiaothong\luatgiaothongController@showList')->name('list');
Route::get('/detail','luatgiaothong\luatgiaothongController@showDetail')->name('detail');
});
?>
luatgiaothongController.php<?php
namespace App\Http\Controllers\luatgiaothong;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use DB;
class luatgiaothongController extends Controller
{
function showList(Request $request){
if (isset($request->title)) {
$list= DB::table('luatgiaothong')
->where('title','like','%'.$request->title.'%')
->paginate(10);
}else{
$list= DB::table('luatgiaothong')->paginate(10);
}
return view('luatgiaothong.list')->with([
'list' => $list,
'index'=>1
]);
}
function showDetail(Request $request){
if (isset($request->id)) {
# code...
$detail = DB::table('luatgiaothong')
->where('id',$request->id)
->first();
}
return view('luatgiaothong.detail')->with([
'detail' => $detail
]);
}
}
2020_07_17_065148_create_luatgiaothong_table.php<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateLuatgiaothongTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('luatgiaothong', function (Blueprint $table) {
$table->increments('id');
$table->string('title', 100);
$table->string('content', 500);
$table->integer('fine_min');
$table->integer('fine_max');
$table->date('created_at');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('luatgiaothong');
}
}
detail.blade.php<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>admin</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
</head>
<body>
<header>
<nav>
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link " href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link active" href="#">Admin</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#">Disabled</a>
</li>
</ul>
</nav>
</header>
<div class="container">
<br>
<br>
<p style="font-size: 50px;text-align: center">{{$detail->title}}</p>
<br><br>
<p>mức phạt thấp nhất : {{$detail->fine_min}}</p>
<p>mức phạt cao nhất : {{$detail->fine_max}}</p>
<h2>Nội dung luật</h2>
<p>{{{$detail->content}}}</p>
<h2>Có hiệu lực từ ngày : {{$detail->created_at}}</h2>
<a href="{{ route('list') }}" class="btn btn-primary">Quay lại</a>
</div>
</body>
</html>
luatgiaothongSeeder.php<?php
use Illuminate\Database\Seeder;
class luatgiaothongSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
//
for ($i=0; $i < 50; $i++) {
# code...
DB::table('luatgiaothong')->insert([
'title' => 'luat giao thong '.($i+1),
'content' => Str::random(100),
'fine_min' => $i*1000,
'fine_max' => $i*100,
'created_at'=>'2020-01-07'
]);
}
}
}
list.blade.php<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>admin</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
</head>
<body>
<header>
<nav>
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link " href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link active" href="#">Admin</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#">Disabled</a>
</li>
</ul>
</nav>
</header>
<div class="container">
<form action="" method="GET">
<div class="input-group mb-3">
<input type="text" class="form-control" placeholder="Search" name="title">
<div class="input-group-append">
<button class="btn btn-success" type="submit">Go</button>
</div>
</div>
</form>
<table class="table table-bordered">
<thead>
<tr>
<th>STT</th>
<th>Tên luật giao thông</th>
<th>Ngày hiệu lực</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach ($list as $item)
<tr>
<td>{{$index++}}</td>
<td>{{$item->title}}</td>
<td>{{$item->created_at}}</td>
<td> <a type="button" href="{{ route('detail') }}?id={{$item->id}}" class="btn btn-primary">Chi tiết</a> </td>
</tr>
@endforeach
</tbody>
</table>
{{ $list->links() }}
<br>
<a href="{{ route('addQuestion') }}" class="btn btn-primary">ADD QUESTION</a>
</div>
</body>
</html>