简体   繁体   English

如何在PHP类中重新定义变量值并将其用于其他方法?

[英]How to redefine variable value and use value to other method in class PHP?

I can't get value variable in a method, after use this value to another method in one class PHP Laravel. 在一个类PHP Laravel中将该值用于另一个方法后,无法在方法中获取值变量。

public function paymentCourse(Request $request)
{

    $idCourse = $request->input('idcourse');
    $CoursePrice = Course::where('id', $idCourse)->first();

    if(Auth::user()->palate) {

        if(Auth::user()->palate->is_palate == 1) {
             // this value I want to transfer method getTransactionAmount()
            $currentPrice = $CoursePrice->price_palate;
        }

    } else {
         // this value I want to transfer method getTransactionAmount()
        $currentPrice = $CoursePrice->price;
    }

    return view('pages.payment-course', compact('currentPrice'));

}



public function getTransactionAmount($type) {

    switch ($type) {
        case 'palate':
            return 11645;
            break;
        case 'course':            
            return 15000; // I get value method paymentCourse() $currentPrice.
            break;
        case 'course_sale':                
            return 12000; // I get value method paymentCourse() $currentPrice.
            break;
        default:
            throw new \Exception('Wrong transaction type');
    }

}

This is my solution, thank you for help. 这是我的解决方案,谢谢您的帮助。

public function paymentCourse(Request $request)
{

    $idCourse = $request->input('idcourse');
    $CoursePrice = Course::where('id', $idCourse)->first();

    if(Auth::user()->palate) {

        if(Auth::user()->palate->is_palate == 1) {
            $currentPrice = $CoursePrice->price_palate;
            \Session::put('currentPrice', $currentPrice);
        }

    } else {
        $currentPrice = $CoursePrice->price;
        \Session::put('currentPrice', $currentPrice);
    }


    return view('pages.payment-course', compact('currentPrice'));

}

private function getTransactionAmount($type) {

    $currentPrice = \Session::get('currentPrice');

    switch ($type) {
        case 'palate':
            return 11645;
            break;
        case 'course':
            return $currentPrice;
            break;
        case 'course_sale':
            return $currentPrice;
            break;
        default:
            throw new \Exception('Wrong transaction type');
    }

}

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

相关问题 在PHP中,在为类定义公共变量时,如何使用变量作为key => value对中的值? - In PHP, how can I use a variable as the value in a key=>value pair when defining a public variable for a class? 如何在其他页面中使用返回变量值 - how use return variable value in other page PHP OOP。 如何获取类的常量__METHOD__的值,该类在另一个类中创建另一个类的实例? - PHP OOP. How to get value of constant __METHOD__ of class what create an instance of other class inside that other class? PHP:如何将变量值更改为另一个类 - PHP: How to change variable value to another class PHP如何为类中的变量设置默认值? - PHP how set default value to a variable in the class? 将值返回的函数存储在if语句的变量中,并在其他条件下使用-php - storing value return by function in variable of if statement and use in other condition -php 如何使用PHPUnit测试一个方法调用同一个类的其他方法,但是没有返回值 - How to use PHPUnit to test a method that calls other methods of the same class, but returns no value 从php类的内部方法访问变量值 - Accessing variable value from inside method of php class PHP使用$ this-> variable作为类方法参数默认值 - PHP Using $this->variable as Class Method Parameter Default Value 如何在JavaScript中使用PHP变量值? - How to use PHP variable value in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM