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') }}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程