dsdsds12222
2016-12-21 13:49
浏览 87
已采纳

Laravel - 在会话中存储多个字段

Currently this is storing only first_name in session. I need to store some other fields which are in the same database row like user_role and city. How can I do that ?

DashboardContaroller.php

public function getIndex( Request $request )
    {
        $this->data['firstNames'] = \DB::table('tb_users')->orderBy('first_name')->lists('first_name', 'first_name');
        Session::put('firstName', $request->get('first_name'));     
        return view('dashboard.index',$this->data);
    }

index.blade.php

<form action="" method="post">
{!! Form::select('first_name', $firstNames) !!}
<button type="submit" value="Submit">Go</button>
</form>

View

<p>{{Session::get('firstName','default value')}}</p>

图片转代码服务由CSDN问答提供 功能建议

目前,这只是在会话中存储first_name。 我需要存储一些其他字段,这些字段位于同一个数据库行中,如user_role和city。 我该怎么办?

DashboardContaroller.php

  public function getIndex(Request $ request)\  n {
 $ this-&gt; data ['firstNames'] = \ DB :: table('tb_users') - &gt; orderBy('first_name') - &gt; lists('first_name','first_name'); \  n Session :: put('firstName',$ request-&gt; get('first_name'));  
返回视图('dashboard.index',$ this-&gt;数据); 
} 
   
 
 

index.blade.php

 &lt; form action =“”method =“post”&gt; 
 {!!  Form :: select('first_name',$ firstNames)!!} 
&lt; button type =“submit”value =“Submit”&gt; Go&lt; / button&gt; 
&lt; / form&gt; 
   
 
 

查看

 &lt; p&gt; {{Session :: get('firstName','default value'  )}}&lt; / p&gt; 
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • douci1918 2016-12-21 13:54
    已采纳

    You can store the data as array:

    Session::put('user', ['first_name' => $request->get('first_name'), 'user_role' => Auth::user()->user_role, 'city' => Auth::user()->city]);
    

    View:

    <p>{{Session::get('user')['city']}}</p>
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongluxin2452 2016-12-21 13:52

    You can store the object of the user, for example

    public function getIndex( Request $request )
    {
        $this->data['firstNames'] = \DB::table('tb_users')->orderBy('first_name')->lists('first_name', 'first_name');
        Session::put('userData', ['firstName' => $request->get('first_name'), 'id' => $request->get('id')]);     
        return view('dashboard.index',$this->data);
    }
    
    评论
    解决 无用
    打赏 举报
  • dongyi5570 2016-12-21 14:00

    Here You Go

    DashboardContaroller.php

    public function getIndex( Request $request )
    {
        $this->data['firstNames'] = \DB::table('tb_users')->orderBy('first_name')->lists('first_name', 'first_name');
        Session::put('user', $request->all());     
        return view('dashboard.index',$this->data);
    }
    

    In your View either iterate over the data like this:

    @foreach (Session::get('user') as $user)
         {{$user}}
    @endforeach
    

    Or use specific keys like

    {{Session::get('user')['first_name']}}
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题