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'
}
}
]
CodePudding user response:
From Official Docs You can read more
$user = User::find(1);
return $user->toJson();
return $user->toJson(JSON_PRETTY_PRINT);
CodePudding user response:
Try this
use Illuminate\Support\Facades\Response;
$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);