Laravel Livewire仅向特定用户发送消息

aiazj4mn  于 2023-02-10  发布在  其他
关注(0)|答案(1)|浏览(126)

我接管了一个正在工作的聊天应用程序。要发送消息,原始代码使用用户列表,然后检查消息是否存在。在这种情况下,它将重定向到聊天刀片。如果不存在,将创建一个新消息,然后重定向到聊天刀片。
Github到应用程序https://github.com/thee-king-yodah/laravel-livewire-chat-message-application
我想更改这个设置,在个人资料页面上使用一个按钮,但是我目前正在进行实现,遇到了问题。如果我在我的刀片服务器中添加livewire,它会为所有用户列出一个按钮。
控制器:创建聊天

namespace App\Http\Livewire\Chat;

use Livewire\Component;
use App\Models\User;
use App\Models\Conversation;
use App\Models\Message;
use GuzzleHttp\Promise\Create;
use Illuminate\Support\Carbon;
use App\Http\Livewire\Chat\ChatList;

class CreateChat extends Component
{
    public $users;
    public $message= 'Start';

    public function checkconversation($receiverId)
    {
        //dd($this->spieler);
        $checkedConversation = Conversation::where('receiver_id', auth()->user()->id)
            ->where('sender_id', $receiverId)
            ->orWhere('receiver_id', $receiverId)
            ->where('sender_id', auth()->user()->id)->get();

        if (count($checkedConversation) == 0) {

            //dd('no conversation');
            $createdConversation= Conversation::create(['receiver_id'=>$receiverId,'sender_id'=>auth()->user()->id,'last_time_message'=>Carbon::now()]);
            $createdMessage= Message::create(['conversation_id'=>$createdConversation->id,'sender_id'=>auth()->user()->id,'receiver_id'=>$receiverId,'body'=>$this->message]);

            $createdConversation->last_time_message= $createdMessage->created_at;
            $createdConversation->save();
            return redirect()->to('/chat');

            //dd($createdMessage);
            //dd('saved');

        } else if (count($checkedConversation) >= 1) {

            return redirect()->to('/chat');
            //dd('conversation exists');
        }
        # code...
    }

    public function render()
    {

        $this->users = User::where('id','!=',auth()->user()->id)->get();
        return view('livewire.chat.create-chat');

    }

Create-chat.blade

<div>
    <div class="flex justify-center">
        <div class="bg-white rounded-lg border border-gray-200 w-96 text-gray-900">
            <ul role="list" class="">
                @foreach($users as $user)
                    <div class="flex justify-center mb-2 mt-4">
                    <button type="button" class="inline-block align-middle text-center select-none border font-normal whitespace-no-wrap rounded py-1 px-3 leading-normal no-underline text-blue-600 border-blue-600 hover:bg-blue-600 hover:text-white bg-white hover:bg-blue-600 ms-1"
                            wire:click='checkconversation({{ $user->id }})'>Nachricht</button>
                    </div>
                @endforeach
            </ul>
        </div>
    </div>
</div>

控制器配置文件

amespace App\Http\Livewire;

use Livewire\Component;
use App\Models\User;
use Illuminate\Support\Carbon;

class Playerprofil extends Component
{
    public $player;

    public function render()
    {
        return view('livewire.playerprofile', [
            'player' => $this->player,
    ]);
    }

    public function mount($id)
    {
        $this->player= User::find($id);
    }

Profil.blade

<div>
                        @livewire('chat.create-chat' )
                    </div>

如果有人能帮助我,我将非常感激。

wnrlj8wa

wnrlj8wa1#

已经解决了。
控制器:CreateChat已添加装载

public function mount()
    {

        $this->users = User::find($this->user_id);
    }

删除了CreateChat.blade中的foreach

<div>
@livewire('chat.create-chat', ['user_id' => $player->id ])
</div>

相关问题