老师好, 11月5日作业,练习一下blade模板的语法。如:变量渲染、foreach。 最近事很多,比较忙,但是还是会忙里偷闲看录播以及补写作业的,老师辛苦了,感恩感恩,虽然我进度落下来了,但是还会抽空推进,该做的作业,该交的毕设计都不会少的。

Controller:
namespace App\Http\Controllers;
use App\Models\Lite;
use Illuminate\Support\Facades\DB;
class Home2 extends Controller
{
public function index(Lite $lite)
{
$data['list'] = $lite->getAll();
return view('db', $data);
}
}
Model:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Lite extends Model{
protected $table = 'lite';
protected $primaryKey = 'id';
public function getAll(){
//return $this->get()->toArray();
return $this->get()->all();
//return $res = \DB::table('lite')->get();
}
}
View:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<table border="1" cellspacing="0" align="center">
<tr bgcolor="#faebd7">
<th>ID</th>
<th>NAME</th>
<th>DESCRIPTION</th>
<th>PRICE</th>
</tr>
@foreach($list as $li)
<tr>
<td>{{ $li->id }}</td>
<td>{{ $li->name }}</td>
<td>{{ $li->description }}</td>
<td>{{ $li->price }}</td>
</tr>
@endforeach
</table>
</body>
</html>
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号