从laravel php类更改静态变量的值

时间:2016-08-24 13:18:51

标签: php class object laravel-5.2

下面提到的代码工作正常,但我想从数据库设置public static $averageEvaluationCount = 3;,以便我可以动态更改$averageEvaluationCount

<?php

namespace App\Models;

use DateTime;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;

class Post extends BaseDBModel
{
    public function __construct()
    {
        $evalCountQuery = DB::table('averageEvaluations')->select('times')->get();
        $evalCounts = $evalCountQuery[0]->times;

    }

    public static $x = 3;
    protected $table = 'posts';
    public static $pointsPerKool = 10;
    public static $averageEvaluationValue = 8;
    public static $averageEvaluationCount = 3; //I want to change this value 3 to a value from database $evalCounts
    protected static $intervalForRedPoint = 1;
    public static $commentRequiredLowThreshold = 10;
    protected static $intervalForBluePoint = 3;
    protected static $rateThresholdToMarkAsGoldWithK = 8;
    protected static $daysToFreeze = 4320;
}

1 个答案:

答案 0 :(得分:1)

public function __construct()
{
    $evalCountQuery = DB::table('averageEvaluations')
        ->select('times')->get();
    self::$averageEvaluationCount = $evalCountQuery[0]->times;
}

OR

public function __construct()
{
    self::$averageEvaluationCount = DB::table('averageEvaluations')
        ->select('times')
        ->first()
        ->times;
}