如何在Laravel 5.6中使用DEFINE()?

时间:2018-09-21 15:58:54

标签: php laravel

我在const.php中有代码:

define('COLORS',array('black','red','white'));

在页面add.blade.php中:

<select  id="example-multiple-selected" multiple="multiple" name="ProductColor[]" >
 <?php foreach (COLORS as $colorKey => $colorName) :?>
  <option value="{{$colorKey}}"
     @if (old("ProductColor"))
       {{ (in_array($colorKey, old("ProductColor")) ? "selected":"") }}
     @endif>{{$colorName}}</option>
 <?php endforeach; ?>
</select>

//示例(获取2个值(“红色”,“白色”)

然后,在页面控制器中,将其添加到数据库中:

$color = implode(',',$products->color);

//保存到DB(1,2)

我将其显示:

@foreach(COLORS as $colorKey => $colorName)
  @if($product->color == $colorKey) {{$colorName}}
  @endif
 @endforeach

///仅显示为:红色

1 个答案:

答案 0 :(得分:0)

您更改文件composer.json

并在自动加载中添加索引文件

样本:

    "autoload": {
        "psr-4": {
            "App\\": "app/"
        },
        "files": [
            "app/helpers/constants.php"
        ]
    },

并在项目路径中运行命令:

composer dump-autoload