簡體   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