dow98764 2015-10-29 03:52
浏览 718

排除Laravel会从模型结果中追加值

Laravel has the option to add an $appends array to each model making additional values automatically available as if they are database attributes by adding accessors for each.

This is normally pretty handy, except in this case I need to ONLY get the fields I put into select() because DataTables is expecting only what I send to it.

Example:

Item::select(['image', 'name', 'color']);

Will return appended fields after color in the attributes.

How do I force the exclusion of the appends values when returning results?

Or alternatively, how do I get DataTables to ignore certain attributes?

Not sure which is the least time costly route.

Currently using yajra/laravel-datatables package to send data to the jQuery DataTables AJAX request.

  • 写回答

2条回答 默认 最新

  • duanlu0386 2015-10-29 06:19
    关注

    You can call each function in the collection object and then use setHidden method to exclude the unwanted fields like this

    $item= Item::select(['image', 'name', 'color'])->get()->each(function($row){
                        $row->setHidden(['appendedField1', 'appendedField2']);
                    });
    

    And for the yajra/laravel-datatables you can use something like

    $item= Item::select(['image', 'name', 'color']);
    return Datatables::of($item)->remove_column('appendedField1');
    
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器