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 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教