0

I am having a controller that returns a JSON value from the database,

My Controller:

<?php

namespace App\Http\Controllers;

use Auth;
use DB;
use App\EmployeeDetail;

class MyController extends Controller
{
    public function index()
    {
        $employeeDetail = new EmployeeDetail();
        if (Auth::check())
        {
            $id = Auth::user()->id;
            $employeeDetail = DB::table('employee_details')->where('user_id', $id)->first();
            dump($employeeDetail);
        }
        return view('myView', compact('employeeDetail'));
    }
}

JSON value of $employeeDetail:

{
  "id": 1
  "emp_id": "1617"
  "content": "{"empId": "1617", "field1": "1111", "field2" : "2222"}",
  "created_at": "2018-05-12 18:09:52"
  "updated_at": "2018-05-12 18:39:30"
}

I need to know how to read the value field1 and field2 in the above JSON in my blade file. I can able to read the emp_id by {{ $employeeDetail->emp_id }}.

Sivanandam
  • 59
  • 10
  • Since the question was unfortunately closed, here my answer. Put `protected $casts = ['content' => 'json'];` in `EmployeeDetail`. Then use `$employeeDetail = EmployeeDetail::where('user_id', $id)->first();` and `{{ $employeeDetail->content['field1'] }}`. – Jonas Staudenmeir May 12 '18 at 19:38
  • @JonasStaudenmeir - Thank you so much!!! It works for me. – Sivanandam May 12 '18 at 19:48

0 Answers0