【问题标题】:Refactor:laravel controller array重构:laravel 控制器数组
【发布时间】:2022-01-18 04:51:08
【问题描述】:
protected $status = array(
        'sale' => [
            'background' => 'bg-info-light',
            'color' => 'text-info'
        ],
        'completed' => [
            'background' => 'bg-success-light',
            'color' => 'text-success'
        ],
    );

我已经定义了数组,并且

$background = $this->code_status[$status]['background'];
            $color = $this->code_status[$status]['color'];
            return "<span class='fw-semibold d-inline-block py-1 px-3 rounded-pill " . $background . " " . $color . "'>$status</span>";

使用的是数据表,但我还有 20 个状态,并且必须为每个状态定义,如果继续写入 20 个数组,我的控制器会很长,我怎样才能更好地重写它?

【问题讨论】:

    标签: arrays laravel database eloquent datatable


    【解决方案1】:

    在我看来,使用语言环境怎么样?

    这是我在制作网页管理页面或服务页面时的个人解决方案。

    例如 'resources/lang/ko/my_resource.php'

    <?php
    
    return  [
            'background' => [
                'status' => [
                    'sale' => 'bg-info-light',
                    'completed' => 'bg-success-light',
                ]
                ],
            'color' => [
                'status' => [
                    'sale' => 'text-info',
                    'completed' => 'text-success',
                ]
            ]
        ];
    

    还有,您的视图文件...

     @foreach(collect(['sale', 'complete']) as $status)
      <span class="fw-semibold d-inline-block py-1 px-3 rounded-pill {{ __('my_resource.background.staus.'.$status) }} {{ __('my_resource.color.status.'.$status) }}">{{ strtoupper($status) }}</span>
      @endforeach
    

    但是,请小心设置您的语言环境和语言环境文件。

    对不起,我的语言不好。这是我在 stackoverflow 中的第一篇文章。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-13
      • 1970-01-01
      • 2017-08-25
      • 2019-04-30
      • 2013-12-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多