Crafting an innovative design and logic for a dynamic chatroom using Laravel and Vue

I'm currently developing a private chatroom feature on my Laravel app. Let me walk you through how it functions - there's a button that triggers a bootstrap modal, and inside that modal, I've embedded a Vue component. Everything seems to be functioning properly, however, I'm facing an issue. How can I display the messages I sent on the left side and the replies on the right? Additionally, I'm encountering problems with some text overlapping the wrapper.

Modal

<div id="messagemodal">
    <userprofilemessagemodal-component 
     :chatroom_id="{{ $chatroom->id }}" 
     :user_id="{{ Auth::user()->id }}" 
     :receiver="{{ $data->id }}"
    >
    </userprofilemessagemodal-component>
  </div>
  

Vue Component

<div class="container-fluid p-3 roombody">
   <div v-for="roommsg in roommsgs" v-bind:key="roommsg.id">
     <div class="container">                        
       <p id="roommsg" class="rounded-top rounded-bottom p-1 text-white"> 
         {{ roommsg.message }} 
       </p>                                       
     </div>                    
   </div>
 </div>
  

Fetched Array Object Structure

id: '',
message: '',
chatroom_id: '',
user_id: '',
receiver_id: '',
created_at: '',
updated_at: '',
  

Is it possible to achieve all this through styling alone? Appreciate your help! PS. I haven't included all the code snippets, just the ones I believe are essential for understanding. Let me know if you need more clarification :)

Answer №1

If you're working with a Vue component, you can handle CSS classes in this way. Remember that you'll need to write the necessary CSS for this to work properly.

<div class="container-fluid p-3 roombody" :class="sender_participant">
    <div v-for="roommsg in roommsgs" v-bind:key="roommsg.id">
        <div class="container">                        
            <p id="roommsg" class="rounded-top rounded-bottom p-1 text-white"> {{ roommsg.message }} </p>                                       
        </div>
    </div>
</div>

For the script part:

export default {
    ....
    computed :{
        sender_participant(){
           return this.user_id==this.logged_in_user ? 'right' : 'left';
        }
    },
}

You should verify whether the sender is the logged-in user. The example above assumes this.logged_in_user contains the ID of the current user.

There are many ways to approach this, but this method is straightforward for beginners to understand.

Answer №2

Great News!


I have discovered a solution to the issue I was facing in my chatroom and wanted to share it with others who may be experiencing the same problem.

Vue Component

<!-- this code is for displaying messages of logged-in user -->
<div id="roommsguser" v-if="roommsg.user_id === user_id">
<p><span>  {{ roommsg.message }} </span></p>
<p id="roommsgtime"> <i> {{ roommsg.created_at }} </i> </p>
</div>

 <!-- this code is for displaying messages of other users -->
<div id="roommsgguest" v-else>
<p><span>  {{ roommsg.message }} </span></p>
<p id="roommsgtime"> <i> {{ roommsg.created_at }} </i> </p>
</div>

Next, in my CSS styling:

CSS

#roommsguser{
    text-align: right;  
}

#roommsguser span{
    color: white;
    padding: 5px;
    background-color: #0080ff;
    border-radius: 30px;
}

#roommsgguest{
    text-align: left;  
}

#roommsgguest span{
    color: white;
    padding: 5px;
    margin: 3px;
    background-color: #14487C;
    border-radius: 30px;
}

#roommsgtime{
    font-size: 60%;
    color: #ececec;
}

Although I'm not entirely certain if this is the most optimal approach, it has proven effective on my end :)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Solving the Issue in Laravel: Resolving the Error 'Attempting to access the country property of a non-object'

Exception Code Appears in profile_settings.blade.php Whenever users visit their profile settings, they encounter stack trace errors with an exception. Can someone please assist? Thank you. $('select[name=country]').val('<?php echo e($use ...

Choose each day's time slot on the jQuery FullCalendar version 2.x

The code snippet below is a segment of the code generated by the FullCalendar jQuery plugin version 2. It has undergone some changes from version 1.x in terms of the classes it utilizes. <div class="fc-slats"> <table> <tbody> ...

Are CSS generated elements limited to text content only?

Can CSS generated content be used for more than just text? Take for example the code snippet below, where the span is displayed as text rather than creating a new span element. .addbefore:before { content: "<span>dfsd</span>"; } ...

Issues with the play() method in Javascript across Firefox, Safari, and IE 11 are causing functionality problems

I developed a basic video and audio player that smoothly fades out an introductory image and starts or stops playing an mp4 file upon click. Everything functions properly in Chrome, but unfortunately does not work in other major browsers. Despite checking ...

Eliminate the space between the navigation bar and carousel when using Materialize with Vue.js and Laravel

I am facing an issue with the gap between the navbar and carousel in my materialize design. I have implemented Vue components for each div and Laravel as the backend. Using Sass for CSS preprocessing, I attempted to adjust the margins for the body, navbar, ...

Can you provide some examples of CSS code snippets?

Can someone share some CSS codes similar to :hover that can be used in the : part for different effects? I tried looking it up but couldn't find much, so any help would be greatly appreciated. ...

Oops! There was an unhandled error: TypeError - Unable to retrieve property 'data' as it is undefined

I'm encountering an issue while working on my vue.js project. Uncaught (in promise) TypeError: Cannot read property 'data' of undefined Let's take a look at the code snippet where the error is happening. In this piece of code, I am ...

Troubleshooting: Inability to use the v-html attribute in VueJS

Currently, I am facing a situation where I need to replace a link within a paragraph element using v-html. Here is the code snippet for reference: <p v-html="websiteHTML"></p> The variable websiteHTML contains the following HTML code: <a ...

Vue's watch function failing to trigger

Experiencing issues with Vue watch methods not triggering for certain objects even when using deep:true. Within my component, I am passed an array as a prop containing fields used to generate forms. These forms are dynamically bound to an object named cru ...

vuex causing data loss when the page is refreshed

Currently, I am working on setting up an authentication system using Laravel and Vue.js. I have implemented JWT and storing tokens in local storage. Even though I can successfully store user data in the Vuex store and see it in the dev tools area, I am f ...

Activate the router-link on v-tab only when text is clicked on

I have a basic v-tab component with a router link: <v-tab> <router-link class="normalize font-weight-bold" to="/docs" > Docs </route ...

What are some possible solutions for resolving the CORS issue with proxies in a production environment?

I recently completed an app using Vue.js with Spring Boot on the backend. During development, I utilized a devServer proxy setup like this: devServer: { proxy: { '/api': { target: 'http://localhost:8087/', changeOrigin ...

Modify the button's background color for Django's image upload field

In one of the forms in my Django application, I have included a field that allows users to upload an image. Here is the code for this field: image = models.ImageField(upload_to=upload_location, null=True, blank=True) To display this field in my form tabl ...

Steps for setting up a variable for a slot in Vuetify:

Here's a photo of my table All rows on my table should be shown by default. I am currently using this slot: <template v-slot:item="{ item, expand, isExpanded }"> Can someone advise me on how to set the isExpanded variable to true initially? ...

Text with gradient overlays

After successfully implementing a gradient effect on an image, I am facing the issue of it covering my text as well. I am struggling to locate the part of the code that needs to be modified in order to prevent the text from being obscured by the gradient e ...

Assistance in configuring Zurb Foundation 5 for optimal integration with Laravel

As a relatively new user to Laravel with previous experience using older versions of Foundation, I am currently in the process of setting up a new Laravel project. My goal is to integrate the Foundation framework into my project, but I find myself a bit co ...

Updating paths to static assets in Vue can help improve the performance of your

I am working with a Vue v2 SPA that I created following the standard application setup outlined in the documentation. The project is named hello-world and after running commands to create and build it, I encountered a requirement: the application must be e ...

The div elements are constantly shifting positions when viewed in Internet Explorer

After completing my website and testing it across different browsers, I noticed an issue with IE. The contact div and navigation shift around the page in this browser specifically. Can anyone help me out with some code to fix this problem? Link to live ex ...

What is causing my HTML display:inline-block to not work properly?

The method of using display: inline-block to arrange div elements next to each other is not effective for my dynamically-generated content cards. My content cards are a customized version of a tutorial from the w3schools website, which can be accessed her ...

What is the best way to delete a CSS class from a specific element in a list using React?

I need to implement a functionality in React that removes a specific CSS class from an item when clicking on that item's button, triggering the appearance of a menu. Here is my code snippet. import "./Homepage.css" import React, { useState, ...