如何将jsonb列值从控制器解析到视图刀片?

时间:2019-01-11 06:13:58

标签: json laravel controller laravel-blade jsonb

我正在开发一个小型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));
    }

2 个答案:

答案 0 :(得分:0)

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

 @foreach($profile as $key => $val)

    <h3> {{ $key }} </h3>

    @foreach($val as $k => $v)

            <p> {{ $k }} - {{ $v }}  </p>
    @endforeach

@endforeach

答案 1 :(得分:0)

已经解决了这个问题,

控制器://

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

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

{{ $profile['work']['designation'] }} 
相关问题