id = $id; $this->payload = $payload; } public function someRandomFillFunction() { $users1 = User::all()->pluck('id'); $users2 = User::all()->pluck('id'); $faker = Faker\Factory::create(); $users1->each( function ($user1, $key) use ($faker, $users2) { $users2->each( function ($user2) use ($faker, $user1) { if ($user1 === $user2) { return; } $data1 = [ 'from_user_id' => $user2, 'to_user_id' => $user1, 'data' => $faker->text(), ]; $data2 = [ 'from_user_id' => $user1, 'to_user_id' => $user2, 'data' => $faker->text(), ]; $newMessage = (new Chat())->forceFill($data1); $newMessage->save(); $newMessage = (new Chat())->forceFill($data2); $newMessage->save(); } ); } ); } } class ChatController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { // $_ = "from_user_id to_user_id"; $curUserId = Auth()->user()->id; $lastChatUsers = Chat::select(['from_user_id', 'to_user_id']) ->where('from_user_id', $curUserId) ->orWhere('to_user_id', $curUserId) ->get() ->map(fn ($e) => [$e['from_user_id'], $e['to_user_id']]) ->flatten() ->unique() ->filter(fn ($e) => $e !== $curUserId) ->values() ->map(fn ($id) => User::find($id)); $lastChatMessage = Chat::withLastUserMessage($curUserId); $lastChatRecipientUserId = $lastChatMessage->count() ? $lastChatMessage['from_user_id'] : null; if (! is_null($lastChatRecipientUserId) && $lastChatMessage['from_user_id'] === $curUserId) { $lastChatRecipientUserId = $lastChatMessage['to_user_id']; } return view( 'chat.index', [ 'lastChatRecipientUserId' => $lastChatRecipientUserId, 'lastChatUsers' => $lastChatUsers, 'curUserId' => $curUserId, 'chanId' => $this->makeChanName($curUserId, $lastChatRecipientUserId), ] ); } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { ChatMessageUpdated::dispatch(new ChatMessage(time(), 'some text '.md5(time()))); } /** * Store a newly created resource in storage. * * @return \Illuminate\Http\Response */ public function store(Request $request) { } /** * Display the specified resource. * * @param \App\Models\Chat $chat * * @return \Illuminate\Http\Response */ public function show(int $lastChatUser) { $lastChatUser = User::find($lastChatUser); // $_ = "from_user_id to_user_id"; $curUserId = Auth()->user()->id; $lastChatUsers = Chat::select(['from_user_id', 'to_user_id']) ->where('from_user_id', $curUserId) ->orWhere('to_user_id', $curUserId) ->get() ->map(fn ($e) => [$e['from_user_id'], $e['to_user_id']]) ->flatten() ->unique() ->filter(fn ($e) => $e !== $curUserId) ->values() ->map(fn ($id) => User::find($id)); return view( 'chat.index', [ 'lastChatRecipientUserId' => $lastChatUser->id, 'lastChatUsers' => $lastChatUsers, 'curUserId' => $curUserId, 'chanId' => $this->makeChanName($curUserId, $lastChatUser->id), ] ); } /** * Show the form for editing the specified resource. * * @return \Illuminate\Http\Response */ public function edit(chat $chat) { } /** * Update the specified resource in storage. * * @return \Illuminate\Http\Response */ public function update(Request $request, chat $chat) { } /** * Remove the specified resource from storage. * * @return \Illuminate\Http\Response */ public function destroy(chat $chat) { } private function makeChanName($from, $to) { return (int) $from < (int) $to ? $from.'_'.$to : $to.'_'.$from; } }