【问题标题】:How do I parse jsonb column value from controller to my view blade?如何将 jsonb 列值从控制器解析到我的视图刀片?
【发布时间】:2019-06-06 01:33:03
【问题描述】:

我正在开发一个小型 Web 应用程序,它在数据库表中有一个 user_id 列和 jsonbinformation,其中包含用户信息。我想在我的刀片文件中显示Auth::user() 信息。

information 列可能包含更多数组,这里是jsonb 列数据。

{"work": {"info": "work", "company": "Augnitive", "working_to": "2019-01-02", "designation": "Software Engineer", "working_from": "2019-01-01", "responsibilities": "Hello"}, "contact": {"info": "contact", "email": "ki.tushar21@gmail.com", "mobile": "01681654863", "address": "House 156, Sultangonj, Rayer Bazar Dhaka 1209, West Agargaon, West Agargaon", "facebook": "fb.com", "linkedin": "linkedin.com", "citizenship": "Bangladesh"}, "personal": {"bday": "2019-01-01", "info": "personal", "blood": "A(+VE)", "gender": "Male"}, "education": {"info": "education", "edu_type": "SSC", "institute": "PGJHS", "graduation": "2010"}}

我不知道如何在控制器的视图中显示这些数据

    public function index()
    {
        $profile = Auth::user()->profile;
//        return $profile;
        return view('home')->with('profile',json_decode($profile,true));
    }

【问题讨论】:

    标签: json laravel controller laravel-blade jsonb


    【解决方案1】:

    在您的视图文件中尝试此代码。我希望这就是您正在寻找的 -

     @foreach($profile as $key => $val)
    
        <h3> {{ $key }} </h3>
    
        @foreach($val as $k => $v)
    
                <p> {{ $k }} - {{ $v }}  </p>
        @endforeach
    
    @endforeach
    

    【讨论】:

      【解决方案2】:

      这个问题已经解决了,

      控制器://

        public function index()
          {
              $profile = Auth::user()->profile->information;
              return view('home')->with(['profile' => $profile]);
          }
      

      在刀片视图中我显示了这样的数据

      {{ $profile['work']['designation'] }} 
      

      【讨论】:

        猜你喜欢
        • 2017-02-20
        • 2019-11-13
        • 2017-09-03
        • 1970-01-01
        • 2022-10-13
        • 2018-09-03
        • 2014-08-23
        • 2017-02-27
        • 1970-01-01
        相关资源
        最近更新 更多