Laravel - 从会话数组中获取项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49126162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel - Get items from session array
提问by Rainier laan
I'm making a step program and therefore I wanted to use sessions. I have a form which allows the user to fill in some information and then the input values will be put in a session array called "basic_settins" like this
我正在制作一个步骤程序,因此我想使用会话。我有一个表单,它允许用户填写一些信息,然后输入值将被放入一个名为“basic_settins”的会话数组中,如下所示
public function saveSettings(Request $request, Product $products)
{
$request->session()->push('basic_settings', $request->input());
return redirect('/themes');
}
But how can I get a specific item from that array and display it for instance in my view? By using something like this: {{ Session::get('store_name') }}
in an input field.
但是如何从该数组中获取特定项目并在我的视图中显示它?通过使用这样的东西:{{ Session::get('store_name') }}
在输入字段中。
The view looks like this:
视图如下所示:
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-12">
<form method="POST" action="{{ route('saveBasicSettings') }}">
<div class="row">
<div class="col-md-12">
<button class="btn btn-primary mb-3 float-right">Next</button>
</div>
</div>
<div class="card z-depth-2" style="border-radius: 7px;">
<div class="card-body">
<input type="hidden" name="user_id" value="{{ Auth::user()->id }}">
{{ csrf_field() }}
<div class="form-group">
<label for="store_name">Store name</label>
<input type="text" class="form-control" placeholder="Store name" id="store_name" name="store_name" value="{{ Session::get('store_name') }}" required>
</div>
<div class="form-group">
<label for="company name">Company name</label>
<input type="text" class="form-control" placeholder="Company name" id="company_name" name="company_name" value="{{ Session::get('company_name') }}" required>
</div>
<div class="form-group">
<label for="company_tel">Company phonenumber</label>
<input type="text" class="form-control" placeholder="Company Phonenumber" id="company_tel" name="company_tel" value="{{ Session::get('company_tel') }}" required>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
@endsection
When I dd the request by doing this: $data = $request->session()->all();
and then dd($data);
当我通过这样做添加请求时:$data = $request->session()->all();
然后dd($data);
I get the following result:
我得到以下结果:
How can I make this work? Thanks in advance!
我怎样才能使这项工作?提前致谢!
采纳答案by Alexey Mezenin
When you're using push()
, an array will be created, so you need to do something like this:
当您使用时push()
,将创建一个数组,因此您需要执行以下操作:
{{ session('basic_settings.0.company_name') }}
Or:
或者:
{{ session('basic_settings')[0]['company_name'] }}
But I would recommend you to save the data with:
但我建议您使用以下方式保存数据:
{{ session(['basic_settings' => request()->all()]) }}
Then you'll be able to read it with:
然后你将能够阅读它:
{{ session('basic_settings.company_name') }}