laravel - php 闭包嵌套 变量如何使用?
大家讲道理
大家讲道理 2017-04-11 10:08:28
[PHP讨论组]


图中代码涉及到闭包嵌套,除了逐级传递还有什么方法使用$data变量嘛

    $data = Conference::pluck('date','date')
                ->flatten(1)
                ->all();

    return Admin::content(function (Content $content) use ($data) {
        $content->header('会议');
        $content->description('列表');
        $content->row(function (Row $row) use ($data) {
            $row->column(6, function (Column $column) use ($data) {
                $column->append((
                    new Box(
                    $data, $this->grid()
                )));
            });
        });
    });
大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

全部回复(4)
巴扎黑
  1. 直接在 return 的最后调用原数据,不用传参

return Admin::content(function (Content $content) {
        $content->header('会议');
        $content->description('列表');
        $content->row(function (Row $row) {
            $row->column(6, function (Column $column) {
                $column->append((
                    new Box(
                    Conference::pluck('date','date')->flatten(1)->all(), $this->grid()
                )));
            });
        });
    });
  1. $this->tempData 代替闭包传值

$this->tempContentData = Conference::pluck('date','date')
                ->flatten(1)
                ->all();
return Admin::content(function (Content $content) {
        $content->header('会议');
        $content->description('列表');
        $content->row(function (Row $row) {
            $row->column(6, function (Column $column) {
                $column->append((
                    new Box(
                    $this->tempContentData, $this->grid()
                )));
            });
        });
    });                
ringa_lee

global $data;?

PHP中文网

写成类吧 直接在函数里面使用$this->data不是很方便?

迷茫

Admin::content() 源代码 不清楚 上下文看应该插入会议详细的功能

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号