猿问

二变量合一变量

我有两个查询。


$createdServer = Server::where('created_at', '>=', \Carbon\Carbon::now()->subMonth())

    ->selectRaw('COUNT(id) as createserver, DATE(created_at) as date,DAY(created_at) as day')

    ->groupBy('day', 'date')

    ->orderBy('date', 'ASC')

    ->orderBy('day', 'ASC')

    ->get();


$deletedServer = Server::where('deleted_at', '>=', \Carbon\Carbon::now()->subMonth())

    ->selectRaw('COUNT(id) as deleteserver, DATE(deleted_at) as date,DAY(deleted_at) as day')

    ->groupBy('day', 'date')

    ->orderBy('date', 'ASC')

    ->orderBy('day', 'ASC')

    ->onlyTrashed()

    ->get();

输出为:


 "deletedServer": [

  {

   "deleteserver": 1,

   "date": "2019-04-29",

   "day": 29

  }

 ],

 "createdServer": [

  {

   "createserver": 2,

   "date": "2019-04-29",

   "day": 29

  }

 ],

我的要求是:


 "allServer": [

  {

   "deleteserver": 1,

   "createserver": 2,

   "date": "2019-04-29",

   "day": 29

  }

 ],

deleteServer和createServer对数据库中的数据进行计数。


慕森卡
浏览 193回答 2
2回答

POPMUISE

merge函数可以解决您当前情况下的问题:链接$merged = $createdServer->merge($deletedServer); $merged->all();编辑:$merged = $createdServer[0]->merge($deletedServer[0]); $merged->all();

沧海一幻觉

您可以使用以下代码进行检索:$allSevers = collect([            "deleteserver" => $deleteServer->count(), // retrieving the deleteServer count            "createserver" => $createServer->count(), // retrieving the createServer count            "date" => date('Y-m-d'), //date format in Year - Month - Date            "day" => date('j') // date format for mday        ]);
随时随地看视频慕课网APP
我要回答