创建数据库
创建名为laravel_db的新数据库。在这个数据库中,创建2个表,名为类别和产品,如下所示。
类别表的结构

类别表的数据

产品表的结构

产品表的数据

数据库关系图

安装Laravel
-
从getcomposer.org/download/下载并安装最新版本的Composer
-
在Visual Studio Code中打开Windows终端,用下面的命令安装Laravel安装程序。
composer global require laravel/installer -
创建新的文件夹,命名为LearnLaravelWithRealApps.使用Visual Studio Code打开LearnLaravelWithRealApps文件夹.在这个文件夹中,创建新的项目,名为LearnLaravelWithRealApps,命令如下。
laravel new LearnLaravelWithRealApps
-
运行LearnLaravelWithRealApps项目,命令如下。
php artisan serve -
打开LearnLaravelWithRealApps项目,网址如下。
http://localhost:8000
连接到数据库
打开根目录下的.env文件.添加如下值,连接到数据库。
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=laravel_db
DB_USERNAME=root
DB_PASSWORD=
清除配置缓存
在Visual Studio Code中打开Windows终端,用下面的命令清除配置缓存。
php artisan config:clear
创建模型
在app/Models文件夹中,创建新的模型,如下所示。
创建分类模型
在app/Models文件夹下创建名为Category.php的新PHP文件,如下所示。
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
protected $primarykey = 'id';
protected $table = 'category';
public $timestamps = false;
public function products()
{
return $this->hasMany(Product::class);
}
}
创建产品模型
在app/Models文件夹下创建名为Product.php的新PHP文件,如下所示。
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
public $primarykey = 'id';
protected $table = 'product';
public $timestamps = false;
public function category()
{
return $this->belongsTo(Category::class, 'category_id');
}
}
创建ProductRepository界面
创建名为Repositories的新文件夹。在Repositories文件夹中,创建名为Product的新文件夹。在app/Repositories/Product文件夹中创建新的PHP文件,命名为ProductRepository.php,如下所示。
<?php
namespace App\Repositories\Category;
interface CategoryRepository
{
public function groupBy();
}
创建CategoryRepositoryImpl类
在app/Repositories/Category文件夹下创建名为CategoryRepositoryImpl.php的新PHP文件,如下所示。
<?php
namespace App\Repositories\Product;
use App\Models\Product;
use Illuminate\Support\Facades\DB;
class ProductRepositoryImpl implements ProductRepository
{
public function groupBy()
{
return Product::select("category_id", DB::raw("count(*) as countProduct"), DB::raw("sum(quantity) as sumQuantity"), DB::raw("min(price) as minPrice"), DB::raw("max(price) as maxPrice"), DB::raw("avg(price) as avgPrice"))->groupBy('category_id')->get();
}
}
声明Category Repository
打开app/Providers文件夹下的AppServiceProvider.php文件,添加声明新的资源库到注册方法,如下所示。
<?php
namespace App\Providers;
use App\Repositories\Category\CategoryRepository;
use App\Repositories\Category\CategoryRepositoryImpl;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->bind(CategoryRepository::class, CategoryRepositoryImpl::class);
}
public function boot()
{
}
}
控制器
在app/Http/Controllers文件夹下创建名为DemoController.php的新PHP文件,如下所示。
<?php
namespace App\Http\Controllers;
use App\Repositories\Category\CategoryRepository;
class DemoController extends Controller
{
protected $categoryRepository;
public function __construct(CategoryRepository $categoryRepository)
{
$this->categoryRepository = $categoryRepository;
}
public function index()
{
$data = array(
'groups' => $this->productRepository->groupBy()
);
return view('demo/index')->with($data);
}
}
观点
在resources/views文件夹下创建名为demo的新文件夹。在这个文件夹中,创建名为index.blade.php的新刀片文件,如下所示。
<html>
<head>
<title>Laravel</title>
</head>
<body>
<h3>Index</h3>
<table border="1">
<tr>
<th>Category Id</th>
<th>Count Product</th>
<th>Sum Quantity</th>
<th>Min Price</th>
<th>Max Price</th>
<th>Avg Price</th>
</tr>
@foreach($groups as $group)
<tr>
<td>{{$group->category_id}}</td>
<td>{{$group->countProduct}}</td>
<td>{{$group->sumQuantity}}</td>
<td>{{$group->minPrice}}</td>
<td>{{$group->maxPrice}}</td>
<td>{{$group->avgPrice}}</td>
</tr>
@endforeach
</table>
</body>
</html>
路由
打开routes文件夹中的web.php文件, 添加新的路由,如下所示:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\DemoController;
Route::get('/', [DemoController::class, 'index']);
Route::get('/demo', [DemoController::class, 'index']);
Route::get('/demo/index', [DemoController::class, 'index']);
Laravel项目的结构

运行应用程序
在Demo控制器中访问index动作,其URL如下。
输出
