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么?

《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
讨论数量: 1

在 Model 中覆写

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

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