繁体   English   中英

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

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

在一个类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');
    }

}

这是我的解决方案,谢谢您的帮助。

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.

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