dragon_9000 2015-09-17 16:24
浏览 58
已采纳

Eloquent在急切加载模型关系时自动选择不需要的列

I am converting an internal API from HTML (back-end) processing to JSON (using Knockout.js) processing on the client-side to load a bunch of entities (vehicles, in my case).

The thing is our database stores sensitive information that cannot be revelead in the API since someone could simply reverse engineer the request and gather them.

Therefore I am trying to select specifically for every relationship eager-load the columns I wish to publish in the API, however I am having issues at loading a model relationship because it seems like Eloquent automatically loads every column of the parent model whenever a relationship model is eager loaded.

Sounds like a mindfuck, I am aware, so I'll try to be more comprehensive.

Our database stores many Contract, and each of them has assigned a Vehicle.

A Contract has assigned an User.

A Vehicle has assigned many Photo.

So here's the current code structure:

class Contract
{
    public function user()
    {
        return $this->belongsTo('User');
    }

    public function vehicle()
    {
        return $this->belongsTo('Vehicle');
    }
}

class Vehicle
{
    public function photos()
    {
        return $this->hasMany('Photo', 'vehicle_id');
    }
}

class Photo
{
    [...]
}

Since I need to eager load every single relationship listed above and for each relationship a specific amount of columns, I need to do the following:

[...]

$query = Contract::join('vehicles as vehicle', 'vehicle.id', '=', 'contract.vehicle_id')->select([
    'contract.id',
    'contract.price_current',
    'contract.vehicle_id',
    'contract.user_id',
    'contract.office_id'
]);

[...]

$query = $query->with(['vehicle' => function ($query) {
    $query->select([
        'id',
        'trademark',
        'model',
        'registration',
        'fuel',
        'kilometers',
        'horsepower',
        'cc',
        'owners_amount',
        'date_last_revision',
        'date_bollo_expiration',
        'bollo_price',
        'kilometers_last_tagliando'
    ]);
}]);

$query = $query->with(['vehicle.photos' => function ($query) {
    $query->select([
        'id',
        'vehicle_id',
        'order',
        'paths'
    ])->where('order', '<=', 0);
}]);

$query = $query->with(['user' => function ($query) {
    $query->select([
        'id',
        'firstname',
        'lastname',
        'phone'
    ]);
}]);

$query = $query->with(['office' => function ($query) {
    $query->select([
        'id',
        'name'
    ]);
}]);

[...]

return $this->response->json([
    'error'           => false,
    'vehicles'        => $vehicles->getItems(),
    'pagination'      => [
        'currentPage' => (integer) $vehicles->getCurrentPage(),
        'lastPage'    => (integer) $vehicles->getLastPage(),
        'perPage'     => (integer) $vehicles->getPerPage(),
        'total'       => (integer) $vehicles->getTotal(),
        'from'        => (integer) $vehicles->getFrom(),
        'to'          => (integer) $vehicles->getTo(),
        'count'       => (integer) $vehicles->count()
    ],
    'banner'          => rand(0, 2),
    'filters'         => (count($input) > 4),
    'filtersHelpText' => generateSearchString($input)
]);

The issue is: if I do not eager load vehicle.photos relationship, columns are loaded properly. Otherwise, every single column of Vehicle's model is loaded.

Here's some pictures so you can understand:

Picture when vehicle.photos isn't eager-loaded.

Picture when vehicle.photos is eager-loaded.


Note: some information have been removed from the pictures since they are sensitive information.

  • 写回答

1条回答 默认 最新

  • donglanfu5831 2015-09-17 21:27
    关注

    You can set a hidden property on your models which is an array of column names you want to hide from being output.

    protected $hidden = ['password'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 (标签-STM32|关键词-智能小车)
  • ¥20 关于#stm32#的问题,请各位专家解答!
  • ¥15 (标签-python)
  • ¥15 第一个已完成,求第二个做法
  • ¥20 搭建awx,试了很多版本都有错
  • ¥15 java corba的客户端该如何指定使用本地某个固定IP去连接服务端?
  • ¥15 activiti工作流问题,求解答
  • ¥15 有人写过RPA后台管理系统么?
  • ¥15 Bioage计算生物学年龄
  • ¥20 如何将FPGA Alveo U50恢复原来出厂设置哇?