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 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制