简体   繁体   中英

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

laravel controller is a normal php class witch extends a BaseController::class

and const is a php feature

ref link https://www.php.net/manual/en/language.oop5.constants.php you can read this

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

<?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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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