skip to Main Content

I am using laravel and mysql ,i am joining two tables based on conditions it’s working fine but my requirement was i have to fetch those details inside json ,can anyone give me some idea how to proceed with this scenario ?

Table1::leftJoin(//mycondition)->select('table1.id','table1.name','table2.*')->get()->toArray();

currentResult

[
{
  'id':1,
  'name':'test',
  'tab2id':1,
  'tab2name':'tab2'
}
]

ExpectedResult

[
{
  'id':1,
  'name':'test',
  'tab2':{
         'tab2id':1,
         'tab2name':'tab2'
         }
}
]

2

Answers


  1. From Official Docs You can read more

    $user = User::find(1);
     
    return $user->toJson();
     
    return $user->toJson(JSON_PRETTY_PRINT);
    
    Login or Signup to reply.
  2. Try this

    use IlluminateSupportFacadesResponse;
    
    $user = User::find(1);
      $json = $user->toJson(JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
    
    or
    echo json_encode($user, JSON_PRETTY_PRINT);
    or
    return Response::json($user, null, null, JSON_PRETTY_PRINT);
    or
    return response()->json($user,200,[],JSON_PRETTY_PRINT);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search