如何在一个使用Laravel的控制器中将数据从一个功能传递到另一个功能?

时间:2019-08-02 08:20:27

标签: php laravel

我有一个帖子,如果发布成功,它将输出data2“ $ result ['STATUS_CODE'] == 200”,如何将新获得的数据传递给另一个函数?

number

这是用于保存来自createPassanger函数的数据的函数,

var useridToken = parseInt(pm.environment.get("useridToken"));

这是getDataSummary函数的视图,我不想在会话中保存数据

public function createPassanger(Request $request){
        $api       = new Api;
        $input     = $request->all();
        $platform  = "W";
        $id_flight = Session::get('id_flight');
        $url       = config::get('constants.url')."api/passanger/add";

        //this is my post
        $post = array(
            "passanger_name"      => $input['name'], 
            "passanger_email"     => $input['email'],
            "passanger_passport"  => $input['passport'],
            "id_flight"           => $id_flight,
            "platform"            => $platform
        );

        $result = $api->post($url, $post);
        //this is results of the post
        if($result['STATUS_CODE'] == 200){
            $summary['id_summary']         = $result['DATA']['id_summary'];
            $summary['id_passanger']       = $result['DATA']['id_passanger'];
            $summary['id_flight']          = $result['DATA']['id_flight'];
            $summary['passanger_name']     = $result['DATA']['passanger_name'];
            $summary['passanger_passport'] = $result['DATA']['passanger_passport'];
            $summary['passanger_email']    = $result['DATA']['passanger_email'];
            $summary['order_id']           = $result['DATA']['order_id'];
            $summary['check_in']           = $result['DATA']['check_in'];
            $summary['status_booking']     = $result['DATA']['status_booking'];
            $summary['status']             = $result['DATA']['status'];
            $summary['member_id']          = $result['DATA']['member_id'];
            $summary['airlines_id']        = $result['DATA']['airlines_id'];
            $summary['airlines_name']      = $result['DATA']['airlines_name'];
            $summary['flight_no']          = $result['DATA']['flight_no'];
            $summary['airport_id']         = $result['DATA']['airport_id'];
            $summary['airport_name']       = $result['DATA']['airport_name'];
            $summary['pnr_ticket']         = $result['DATA']['pnr_ticket'];
            $summary['departure_time']     = $result['DATA']['departure_time'];
            $summary['departure_date']     = $result['DATA']['departure_date'];
            $summary['bag_amount']         = $result['DATA']['bag_amount'];
            $summary['price']              = $result['DATA']['price'];
            $summary['code']               = $result['DATA']['code'];
            $summary['symbol']             = $result['DATA']['symbol'];

            //I don't want to make a session
            // Session::put($summary);

            Session::flash('defaultSuccess', 'Success');
            Session::flash('textDefaultSuccess', ucfirst((strtolower($result['MESSAGE']))));

            return redirect()->route('summary');
        }else{
            Session::flash('defaultFailed', 'Create Summary Failed!');
            Session::flash('textDefaultFailed', ucfirst((strtolower($result['MESSAGE']))));
            return redirect()->route('passanger');
        }

    }

我已经尝试过几次从帖子中获取结果数据以保存到getDataSummary中,但是结果是500错误

2 个答案:

答案 0 :(得分:1)

您可以通过使用此代码

进行尝试
    public function createPassanger(Request $request){
        $api       = new Api;
        $input     = $request->all();
        $platform  = "W";
        $id_flight = Session::get('id_flight');
        $url       = config::get('constants.url')."api/passanger/add";

        //this is my post
        $post = array(
            "passanger_name"      => $input['name'], 
            "passanger_email"     => $input['email'],
            "passanger_passport"  => $input['passport'],
            "id_flight"           => $id_flight,
            "platform"            => $platform
        );

        $result = $api->post($url, $post);
        //this is results of the post
        if($result['STATUS_CODE'] == 200){
            $summary['id_summary']         = $result['DATA']['id_summary'];
            $summary['id_passanger']       = $result['DATA']['id_passanger'];
            $summary['id_flight']          = $result['DATA']['id_flight'];
            $summary['passanger_name']     = $result['DATA']['passanger_name'];
            $summary['passanger_passport'] = $result['DATA']['passanger_passport'];
            $summary['passanger_email']    = $result['DATA']['passanger_email'];
            $summary['order_id']           = $result['DATA']['order_id'];
            $summary['check_in']           = $result['DATA']['check_in'];
            $summary['status_booking']     = $result['DATA']['status_booking'];
            $summary['status']             = $result['DATA']['status'];
            $summary['member_id']          = $result['DATA']['member_id'];
            $summary['airlines_id']        = $result['DATA']['airlines_id'];
            $summary['airlines_name']      = $result['DATA']['airlines_name'];
            $summary['flight_no']          = $result['DATA']['flight_no'];
            $summary['airport_id']         = $result['DATA']['airport_id'];
            $summary['airport_name']       = $result['DATA']['airport_name'];
            $summary['pnr_ticket']         = $result['DATA']['pnr_ticket'];
            $summary['departure_time']     = $result['DATA']['departure_time'];
            $summary['departure_date']     = $result['DATA']['departure_date'];
            $summary['bag_amount']         = $result['DATA']['bag_amount'];
            $summary['price']              = $result['DATA']['price'];
            $summary['code']               = $result['DATA']['code'];
            $summary['symbol']             = $result['DATA']['symbol'];

            //I don't want to make a session
            // Session::put($summary);
            $file = storage_path().'/data.json';
            $handle = fopen($file, 'w+');
            fwrite($handle, '');
            fwrite($handle, json_encode($summary);
            fclose($handle);

            Session::flash('defaultSuccess', 'Success');
            Session::flash('textDefaultSuccess', ucfirst((strtolower($result['MESSAGE']))));

            return redirect()->route('summary');
        }else{
            Session::flash('defaultFailed', 'Create Summary Failed!');
            Session::flash('textDefaultFailed', ucfirst((strtolower($result['MESSAGE']))));
            return redirect()->route('passanger');
        }

    }

并获取数据

    public function getDataSummary(){
          $jsondata = array();
           $file = storage_path() . '/data.json';
        $handle = fopen($file, 'r');
        $size = filesize($file);
        if ($size > 0) {
            $str = fread($handle, $size);
            $jsondata = json_decode($str,true);
        }
        fclose($handle);
        //function to save the post results from function createPassanger
        $data['title'] = "Summary";
        return view('v_summary', compact('jsondata','data'));
    }

答案 1 :(得分:0)

if($result['STATUS_CODE'] == 200){
   self::getDataSummary($result);
}

然后在您的函数中接收此数组

public function getDataSummary($result){
        //Handle your data
        $data['code'] = $result['DATA']['code'];
        return view('v_summary', $data);
}