diff --git a/app/Http/Controllers/Admin/SupplyDemandController.php b/app/Http/Controllers/Admin/SupplyDemandController.php index 273d806..9cd6795 100755 --- a/app/Http/Controllers/Admin/SupplyDemandController.php +++ b/app/Http/Controllers/Admin/SupplyDemandController.php @@ -270,7 +270,7 @@ class SupplyDemandController extends BaseController $interactionGrowthRate = $this->calculateGrowthRate($interactionCount, $prevInteractionCount); // 当期供需发布分页 - $list = SupplyDemand::with(['user', 'messages' => function ($query) { + $list = SupplyDemand::with(['user', 'dialogues' => function ($query) { $query->with('user', 'toUser')->limit(2)->orderBy('created_at', 'desc'); }])->where(function ($query) use ($type) { if ($type) { @@ -278,13 +278,6 @@ class SupplyDemandController extends BaseController } })->whereBetween('created_at', [$startDate, $endDate]) ->paginate($all['page_size'] ?? 20); - foreach ($list as $item) { - $item->dialogue = Dialogue::with('user', 'toUser')->where(function ($query) use ($item) { - $query->where('user_id', $item->user_id)->where('to_user_id', $item->to_user_id); - })->orWhere(function ($query) use ($item) { - $query->where('user_id', $item->to_user_id)->where('to_user_id', $item->user_id); - })->first(); - } return $this->success([ 'list' => $list, 'supply_demand_count' => $supplyDemandCount, diff --git a/app/Models/SupplyDemand.php b/app/Models/SupplyDemand.php index b9c0860..9fb41cd 100755 --- a/app/Models/SupplyDemand.php +++ b/app/Models/SupplyDemand.php @@ -34,5 +34,10 @@ class SupplyDemand extends SoftDeletesModel return $this->hasMany(Message::class, 'supply_demand_id', 'id'); } + public function dialogues() + { + return $this->hasMany(Dialogue::class, 'supply_demand_id', 'id'); + } + }