我正在尝试将数组的值从一个方法传递给Laravel中的另一个方法。基本上我有一个名为 orderingSubmission 的方法从Ajax POST执行中收到一个数组(该部分工作正常),但后来我试图将数组值传递给另一个名为 orderingSubmissionReceipt <的方法/ strong>所以我最终可以在订单摘要页面上呈现视图。我正在使用会话,但它们似乎不起作用。请问有人能告诉我我想念的是什么吗?
这是我的route.php
Route::get('/', function() { return redirect('home'); });
Route::get('home', 'HomeController@home');
Route::post('home', 'HomeController@activateCustomer');
Route::post('order-summary', 'HomeController@OrderingSubmission');
Route::get('order-summary', 'HomeController@orderingSubmissionReceipt');
HomeController.php
public function orderingSubmission(Request $request)
{
$allValues2 = $request->get('users'); // This code works fine. It gets the array
Log::info('This is orderingSubmission.....: ' . print_r($allValues2, true));
Session::set('allValues2',$allValues2);
}
public function orderingSubmissionReceipt()
{
$allValues3 = Session::get('allValues2'); // This code is not getting the session value
Log::info('This is orderingSubmissionReceipt.....: ' . print_r($allValues3, true));
return view('order-summary', [
'people' => $allValues3
]);
}
答案 0 :(得分:4)
您可以使用 $ request-&gt; session-&gt; put()和 $ request-&gt; session-&gt; get(),如下所示:不要忘记在orderingSubmissionReceipt 函数声明中添加 Request $ request。
public function orderingSubmission(Request $request)
{
$allValues2 = $request->get('users'); // This code works fine. It gets the array
Log::info('This is orderingSubmission.....: ' . print_r($allValues2, true));
$request->session()->put('allValues2',$allValues2);
}
public function orderingSubmissionReceipt(Request $request)
{
$allValues3 = $request->session()->get('allValues2'); // This code is not getting the session value
Log::info('This is orderingSubmissionReceipt.....: ' . print_r($allValues3, true));
return view('order-summary', [
'people' => $allValues3
]);
}
答案 1 :(得分:2)
您可以创建类变量,如下所示:
class A
{
private $allValues2 = array();
function orderingSubmission(Request $request)
{
$this->allValues2 = $request->get('users');
//the rest of the code in this method..
}
function orderingSubmissionReceipt()
{
//if you want to make a local variable first, you can do it like this:
$allValues3 = $this->allValues2;
Log::info('This is orderingSubmissionReceipt.....: ' . print_r($allValues3, true));
//or, you can just use the variable directly, like this:
Log::info('This is orderingSubmissionReceipt.....: ' . print_r($this->allValues2, true));
}
}
答案 2 :(得分:1)
如果session()
无效或显示任何错误,您可以使用Session
辅助方法。
public function orderingSubmission(Request $request)
{
$allValues2 = $request->get('users');
session()->set('allValues2', $allValues2);
Log::info('Value set to session: ' . session()->get('allValues2'));
}
public function orderingSubmissionReceipt()
{
Log::info('Value get from session: ' . session()->get('allValues2'));
$allValues3 = session()->get('allValues2');
return view('order-summary', [
'people' => $allValues3
]);
}
答案 3 :(得分:-1)
你应该做那样的事情:
use Illuminate\Http\Request; //You probably already have that!
public function orderingSubmission(Request $request)
{
$allValues2 = $request->get('users');
$request->session()->put('allValues2', $allValues2);
Log::info('Value set to session: ' . $request->session()->get('allValues2'));
}
public function orderingSubmissionReceipt(Request $request)
{
Log::info('Value get from session: ' . $request->session()->get('allValues2'));
$allValues3 = $request->session()->get('allValues2');
return view('order-summary', [
'people' => $allValues3
]);
}