我有2个变量2查询的结果和我有一个JSON
对象我想查询结果append
到JSON,然后它可以返回给用户追加查询结果到一个现有的JSON对象
控制器我所有的结果
if ($request->wantsJson()) {
$agent_id = $request->user()->id;
$drivers_cout = Driver::all()->count();
$drivers_cout_by_agent = Driver::where(['agent_id' => $agent_id])->get()->count();
return $request->user();
}
//the result of $request->user();
#attributes: array:12 [
"id" => 201702
"name" => "Agent"
"email" => "[email protected]"
"phone_number" => "966355826"
]
//and i want something like
#attributes: array:12 [
"id" => 201702
"name" => "Agent"
"email" => "[email protected]"
"phone_number" => "966355826"
"drivers_count" => "96" //How do i append these two
"drivers_cout_by_agent" => "16"
]
谢谢
试试这个:[序列化模型和集合](https://laravel.com/docs/5.3/eloquent-serialization#serializing-models-and-collections) –