Reputation: 3
I took over a working chat application. to send a message, the original code uses a user list, which then checks whether a message exists. In this case, it will redirect to the chat blade. If not, a new message will be created and then redirected to Chat blade.
Github to the app https://github.com/thee-king-yodah/laravel-livewire-chat-message-application
I would like to change this and use a button on a profile page, but I'm currently working on the implementation and get stuck. If I add the livewire into my blade it list a button for all users.
Controller: CreateChat
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>
Controller Profile
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>
I would be very grateful if someone could help m out.
Upvotes: 0
Views: 336
Reputation: 3
It's solved.
Controller: CreateChat added mount
public function mount()
{
$this->users = User::find($this->user_id);
}
removed foreach in CreateChat.blade
<div>
@livewire('chat.create-chat', ['user_id' => $player->id ])
</div>
Upvotes: 0