简体   繁体   English

Laravel 模型常量到控制器中

[英]laravel model constant into controller

I have a model that contains a constant but I want it to move in my controller because the amounts for each key (Basic,Junior,Premium,Advanced,Senior) are being calculated and not fixed.我有一个包含常量的模型,但我希望它在我的控制器中移动,因为每个键(基本、初级、高级、高级、高级)的数量正在计算而不是固定的。 I think moving it inside a controller function I can easily update the amount for each key.我认为将它移动到控制器功能中我可以轻松更新每个键的数量。

User Model:用户模型:

protected const AMOUNTS = [
   'Basic' => 0,
   'Junior' => 100,
   'Premium' => 150,
   'Advanced' => 200,
   'Senior' => 250,
];

and I'm accessing those constants in a function like this我在这样的函数中访问这些常量

protected function get_amount($rank)
{
    $amount = self::AMOUNTS[$rank];
}

I literally don't know how to convert it to fit inside the controller because I want to move in controller because the amount is dynamic and not fixed.我真的不知道如何将它转换为适合控制器,因为我想在控制器中移动,因为数量是动态的而不是固定的。

I want something like in my controller我想在我的控制器中使用类似的东西

// Controller
public function insert()
{
    // Setting the amount for each
    $amounts = [
       'Basic' => $basic_amount,
       'Junior' => $junior_amount,
       'Premium' => $premium_amount,
       'Advanced' => $advanced_amount,
       'Senior' => $senior_amount,
    ];

    // The problem is how to access that, if that's the case?
    // I can't do the same with the model using self::AMOUNTS[$rank];
}

Constants values cant be modified once set, thats the purpose of them, however you can change it into a static array in your controller, and you can change its values when you want :常量值一旦设置就无法修改,这就是它们的目的,但是您可以将其更改为控制器中的静态数组,并且您可以在需要时更改其值:

// Controller
// Controller
protected static $amounts = [
    'Basic' => 0,
    'Junior' => 100,
    'Premium' => 150,
    'Advanced' => 200,
    'Senior' => 250,
];

public function insert()
{
    // Setting the amount for each
    self::$amounts = [
       'Basic' => $basic_amount,
       'Junior' => $junior_amount,
       'Premium' => $premium_amount,
       'Advanced' => $advanced_amount,
       'Senior' => $senior_amount,
    ];

}

and to read a value you can simply do self::$amounts['Junior'] and it will get you the value并读取一个值,你可以简单地做self::$amounts['Junior'] ,它会给你值

laravel controller is a normal php class witch extends a BaseController::class laravel 控制器是一个普通的 php class扩展了一个BaseController::class

and const is a php feature const 是一个 php 特性

ref link https://www.php.net/manual/en/language.oop5.constants.php you can read this参考链接https://www.php.net/manual/en/language.oop5.constants.php你可以阅读这个

so how you will use this in controller is所以你将如何在控制器中使用它

<?php
class MyClass
{

    const AMOUNTS = [
        'Basic' => 0,
        'Junior' => 100,
        'Premium' => 150,
        'Advanced' => 200,
        'Senior' => 250,
    ];


    public function insert()
    {
        $amounts = self::AMOUNTS;
    }
}


and you don't need to move in to controller just create const like this in model并且您不需要进入控制器,只需在模型中创建这样的 const

<?php
class ModelClass
{

    const AMOUNTS = [
        'Basic' => 0,
        'Junior' => 100,
        'Premium' => 150,
        'Advanced' => 200,
        'Senior' => 250,
    ];
}

then in contsoller you can get the data by然后在控制器中,您可以通过以下方式获取数据


public function insert()
{
     ModelClass::AMOUNTS['Basic'];
     $amounts = ModelClass::AMOUNTS; // to get all
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM