dongqiaolong9034
2018-03-06 08:15
浏览 63
已采纳

Laravel - 从会话数组中获取项目

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

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.

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);

I get the following result:

enter image description here

How can I make this work? Thanks in advance!

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongyong8491 2018-03-06 08:22
    已采纳

    When you're using push(), an array will be created, so you need to do something like this:

    {{ 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') }}
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题