Messaging with two user tables in Laravel - laravel

I am developing a system in which it will have a chat system between two user tables: the clients and the engineers. My problem is how to make these two tables communicate in one message table with regard to registering the sender ID and recipient ID since both can change and are in different tables.
My initial thought for this was two tables where I would use the polymorphic relationship:
chats
id
client_id
engineer_id
messages
id
chat_id
senderable_id
senderable_type
receiverable_id
receiverable_type
content
What would be the most recommended way to solve this problem?

First of all, the following its the generic way that I see to create a chat schem. It'll works to a chat with 2 users or more than 2 users.
Note, the user_chat table its the pivot table between users and chats. You can define an alias to that table, like "participants", because it what it means. You can add more attributes to chats table, but basically need to know the creator.
Finally, the secret in messages table is just to get the sender id from users table, because sender and receiver are relative concepts in this context.
Another more simple and reduced way to do it (if you are trying to make a chat with only 2 users) is:

Related

DynamoDB Event Scheduling Table Structure

I'm building out a scheduling app, where users can invite other users to events. However, I'm having a little trouble figuring out how I want to set up my tables.
Since DynamoDB is a NoSQL database we want to try to keep data that is related, in the same table. However, this problem gets a bit more confusing because some events for my app may have roles, such as organizer, planner, etc. Therefore, I want users for an event to be able to accept the invitation to the event with specific roles.
Therefore, in this case, would I want to use two tables, one that stores the event (UUID, Name, etc.) and the other that has the users attending events (UUID of Event, Username, etc.) or use one table?
Plus, when I create my get request to fetch the event data if I use two tables, would I have to query the user table to get the number of people attending that event or should I store that value in the event table, and then when a user signs up, add a new row to the users table, and update the number of people attending in the event table?
For the app I am using Lambda functions on AWS.
Any help would be appreciated, thanks!
You can have
userid as hash-key
eventid as range-key
role as LSI
You can also have a GSI on event-id to query based on eventid
sample table:
user1, event1, organizer
user2, event1, planner
user3, event1, attendee
user4, event1, attendee

Design DB for chats between users

I have one problem with designing database structure for my app. I have 3 models: Events, groups and users.
Relations:
Events - Users - many to many.
Groups - Users - many to many.
There will be available one chat withing group and one inside event. Additonally in the near future I am going to implement chats between two or more users.
I'm a bit confused what is the best way to design it. I created Chat model and many to many relation between chat and users.
First idea: The members of group and event chats will be stored in respectively event_users and group_users tables. Only chats between users will be stored in chat_users.
Second idea: chat_users will be synchronized with event_users and group_users tables. The advantage of this way is that I will have simple separated logic to manage chats and there will be not many complicated queries to DB.
Thanks for any feedback or maybe other ideas.
I believe that Chat has a polymorphic association with Group and Event (and maybe User as well). Some solutions to the problem are provided here:
https://www.slideshare.net/billkarwin/practical-object-oriented-models-in-sql/29-Polymorphic_Assocations_Solutions_Exclusive_arcs
Could something like this work?
ChatUser
id
user_id
chat_id
Chat (exclusive arcs method)
id
event_id (nullable)
group_id (nullable)
description/etc
ChatMessage
id
chat_id
user_id_from
user_id_to (nullable)
date
message
Assumptions made:
event/group can contain multiple chats
user can belong to a group but not to group chat
Besides, Laravel comes with a native polymorphic relation handling solution. It could fit here as well.
User can participate to an event and event can have many users who participate to that event, in this specific case we need to use as Many to Many relation Laravel One to Many Relation
event_user
id
event_id
user_id
User can be member of many group and a group can avec many member, in this case we need a Many to Many relation Laravel Many to Many Relation
group_user
id
group_id
user_id
chat can be related to an event or a group, so we save the type of the chat witch can be either Event or Group and the corresponding ID of that event or group. so if we have a particular chat we can retreive all users related to that chat through the corresponding event or group it depending of the chatable_type in each case, to learn more see Laravel Many To Many Polymorphic Relations
chats
id
name
chatable_id
chatable_type
we need also one table to save all conversations for specific chat
messages
id
chat_id
message
created_at
and for users chat you can create a separate table witch can save the related information about that chat specifiquely
conversations
id
sender_id
receiver_id
message
all sender_id and receiver_id will be foreign key witch reference some id on users table

Laravel: Table structure for multiple users types, polymorphic relationships

In my site (api using laravel 5.6 and laravel passport) I have two types of users (Teachers and Students), in the future there will be more. The teacher and student entities are very different, meaning that if I keep them all in one table, the table will be long and many fields will have a null value. Right now I have one Users table with common fields and two other tables (Teachers and Students) to which I have setup a polymorphic relationship from user. My question is if this is a good approach, or if there are other ways to handle this more elegantly?
I would create 1 table for Teachers and 1 table for Students and not use the Users table/model. This way you can keep them completely separate and not worry about adding more types of users in the future. Continually trying to fit new users into your existing Users model, which would be shared, is a headache. I made this same mistake when I started and eventually had to rework the project.
There are plenty of guides for Laravel multi-auth / multi-user online.
Here are a couple to help you get started:
https://medium.com/hello-laravel/multiple-authentication-system-laravel-5-4-ac94c759638a
https://www.codementor.io/okoroaforchukwuemeka/9-tips-to-set-up-multiple-authentication-in-laravel-ak3gtwjvt
Also, there are cases where it makes sense to use the User model for multiple types of users. For example, you may have multiple roles for a user where most/all of the fields are the same (not your scenario). In this case, you can assign a 'role' to each User and the check the roles for actions (e.g. add middleware to prevent roles from accessing various routes). Here is an example:
https://medium.com/#ezp127/laravel-5-4-native-user-authentication-role-authorization-3dbae4049c8a
Since you said the teacher and student entities are very different, you should keep them separate.

Migrate User Between Tenants

I want to implement a function to migrate a user from one tenant to another.
I know this could be achieved simply by change user's tenant id.
The problem is, I have an order has this user id and tenant id recorded, if I simply updated this user's tenant id, i won't be able to locate the user using the data recorded in the order.
If I have to update all the orders during migrating process, this could take up a long time for a large database.
Since the default PK type for ABP is long, is it possible to replace it as guid or are there any options I could choose?
Thanks.

Correct Many to Many friends relationship for users

What is the correct way to relate users in parse to one another as friends?
I also want users to be able to query other users but not see all fields, like emails. Is there a way I can create a view on the users table with cloud code, and only return this to the client code?
I was thinking I can create a friends table that will have two columns with 2 pointers, 1 for each user.
thanks for any advice.
I was thinking I can create a friends table that will have two columns with 2 pointers, 1 for each user.
I'll do that too, with a status column to handle pending, blocked...etc
I also want users to be able to query other users but not see all fields, like emails.
You have to add a privateData column on user with ACL restricted to owner only, which would contain private infos such as emails...etc

Resources