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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵