一、下载 laravel 5.5
composer create-project laravel/laravel=5.5.* laravel5.5_resource
新建数据库 laravel5.5_resource
修改 .evn 配置文件
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel5.5_resource
DB_USERNAME=laravel5.5_resource
DB_PASSWORD=laravel5.5_resource
修改中国时区,在 config/app.php 中修改
'timezone' => 'PRC',
切换目录
cd laravel5.5_resource
二、创建数据
创建 post model
php artisan make:model Post -m
创建文章 factory
php artisan make:factory PostFactory --model=Post
此命令会生成一个文件:database/factories/PostFactory.php,打开这个文件,进行修改
$factory->define(App\Post::class, function (Faker $faker) {
return [
'title' => $faker->sentence,
'user_id' => 1,
'content' => $faker->paragraph,
];
});
执行数据迁移
php artisan migrate
如果报错
Specified key was too long; max key length is 1000 bytes
问题解决:在 AppServiceProvider 中调用 Schema::defaultStringLength 方法来实现配置:
use Illuminate\Support\Facades\Schema;
public function boot()
{
Schema::defaultStringLength(191);
}
进入 tinker
php artisan tinker
创建用户数据
factory('App\User', 5)->create();
创建文章数据
factory('App\Post', 3)->create();
三、User
创建 User resource
php artisan make:resource User
修改创建的文件:app/Http/Resources/User.php
public function toArray($request)
{
return [
'username' => $this->name,
'email' => $this->email,
];
}
添加自定义字段:app/Http/Resources/User.php 新增方法
public function with($request)
{
return [
'status' => 'success',
];
}
修改 routes/web.php
Route::get('/', function () {
$user = \App\User::find(1);
return new \App\Http\Resources\User($user);
});
启动 serve
php artisan serve
去除 data 参数:修改 app/Providers/AppServiceProvider.php
use Illuminate\Http\Resources\Json\Resource;
public function boot()
{
Resource::withoutWrapping();
}
删除:app/Http/Resources/User.php 新增的 with 方法
自定义 data 名称:修改 app/Providers/AppServiceProvider.php
use Illuminate\Http\Resources\Json\Resource;
public function boot()
{
Resource::wrap('info');
}
四、UserCollection
4.1、collection
创建 UserCollection resource
php artisan make:resource UserCollection
修改创建的文件:app/Http/Resources/UserCollection.php
public function with($request)
{
return [
'status' => 'success',
];
}
修改 routes/web.php
Route::get('/', function () {
$users = \App\User::get();
return new \App\Http\Resources\UserCollection($users);
});
4.2、paginate
修改 routes/web.php
Route::get('/', function () {
$users = \App\User::paginate(2);
return new \App\Http\Resources\UserCollection($users);
});
可以看到 laravel 会自动新增两个参数:links、meta
4.3、模型关系
创建 Post resource
php artisan make:resource Post
创建 PostCollection resource
php artisan make:resource PostCollection
修改 app/Http/Resources/Post.php
public function toArray($request)
{
return [
'title' => $this->title,
];
}
修改 app/Post.php
public function posts()
{
return $this->hasMany(Post::class);
}
修改 app/Http/Resources/User.php
public function toArray($request)
{
return [
'username' => $this->name,
'email' => $this->email,
'posts' => new PostCollection($this->posts),
];
}
修改 routes/web.php
Route::get('/', function () {
$user = \App\User::with('posts')->find(1);
return new \App\Http\Resources\User($user);
});