Bug?Laravel 5.5 使用 API Resources 处理 Notifications 时的小疑问

  • Laravel Version: 5.5
  • PHP Version: PHP 7.1.7 (cli) (built: Jul 15 2017 18:08:09) ( NTS )
  • Database Driver & Version: SQLite version 3.19.3 2017-06-27 16:48:08
  • OS: macOS High Sierra 10.13.2

Description:

对Notifications使用 API Resources 时,发现了一个问题:

Model:

namespace App;

use Illuminate\Database\Eloquent\Model;
class Notification extends Model
{

    protected $table = 'notifications';

}

Resource:


namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\ResourceCollection;

class SystemNotificationCollection extends ResourceCollection
{
    public function toArray($request)
    {
        return [
            'data' => $this->collection,
            'links' => [
                'self' => 'link-value',
            ],
        ];
    }
}

Controller:

public function index(Request $request)
    {
        return new SystemNotificationCollection( Notification::where('type', SystemNotification::class)->paginate());
    }

Database:

image

Result:
image

Transform:
e58f93d1-1c44-4271-8381-0f927b7bdda4 -> 0

20d91d78-0075-4e6e-b7db-ec8fabe1e130 -> 20

所以转换过程类似于 : (int)id ?

我在 Model中添加如下代码:

    public function getIdAttribute($value)
    {
        return $this->attributes['id'];
    }

问题得以解决:
image

所以,这是一个Bug么?

《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 1

在 Model 中覆写

protected $keyType = 'string';
6年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!