Laravel simple private conversation structure - sql

I am trying to create a simple private conversation between two users. I've tried following some examples but all i can find is for multiple users in multiple conversations and more complex versions. My endgoal is for simply be able to do $user->conversations->messages to get all my conversations and their messages that has been sent between two users.
I only need some help with the structure and relation between the tables to easily find correct conversation for each user and the messages connected to the conversation.
I currently have 3 models for this.
User Model
Conversation Model
PrivateMessages Model
My tables currently look like this but i think i need to do some changes to them also.
users:
id
name
conversations:
id
private_messages:
id
sent_by
sent_to
body
read_at
conversation_id

Related

Bigquery - (GA4 data) - why the number of user ID's do not match Unique Users Metrics?

I do have my GA4 property connected to Bigquery. I use my own user_id for tracking - its available once someone logs into my app.
For some reason, when I'm trying to build a report User ID string vs Unique Users, some ID's have more than 1 Unique User reported. Showing an example on the attached image.
Why is that and how I can fix that situation? I'd expect that one ID = One User
User ID vs Number of Users

How can I interrogate user activity in Laravel

I'm looking for a way to best analyse the user data in our app.
for example
how many users have read 0 articles on our site
how many users have read 1 article on our site etc
we have a users table with id, username columns and we also have an activities table that creates an entry when an article is viewed. For example it would create a database row with
id
activity
user_id
1
read
1
all of the data we need is there, I just don't know how to interrogate to give that detail.
I personally would use an int code for activity and not a string. So "read" would = 1, "edit"=2...etc If you have no care about what user read with no association, that becomes easy by just setting a flag on the article that was read by article id. So your article table would have:
id article_id activity user_id
1 5 1 8675309
From there just do an eloquent query on article_id where activity = 1...
$articlecount = Article::where('article_id', $request->id)->where('activity', 1)->count();
$articlecount will give you all of the reads for that article.
If you need it based on user you could do a one to many relationship from your users models to articles models. Then query the user with(articles). That will bring back all the articles where that user has activity. You could also specify in your eloquent to only bring back activity of reads/edits..etc too. In the same respect you could do the reverse and query the article and see all the users that have read that same article.

SQL model follows to retrieve previous notifications

These are my tables:
Author
author_id text
Client
client_id text
Post
author_id text
post_id text
Follow
client_id text
author_id text
I am having trouble modeling the follow table to achieve the following:
1)Client can fetch all the posts from authors which he is currently following
2)Client can follow then unfollow then follow and so on...
3)Whenever an author adds a post, clients receive a notification for the post(this is done through another service(firebase) which doesn't provide a way to fetch previous notifications)
4)Client can fetch all previous notifications he received(i'm having trouble with this point)
Does anybody have an idea how to model my tables to be able to query for the last point.
Thank you
If you want to be able to track the history of a client following an author, rather than just the current state, then you need effective start and end dates on your Follow table.
This will also allow you to track theoretical notifications (i.e. when a notification should have gone out). If you want to track actual notifications then you would need your notification process to record successful notifications in a table.
Your Post table also need a date column

Database Model - SQL - Best Approach

I'm looking for help with part of a database design.
I have to Model the Database for a Group of Contacts and a Group of Distribution Lists.
Each Contact can be in many Distribution Lists and each Distribution List can have many Contacts. In a normal Instance, I could use a Junction table to achieve this solution.
But there's one more thing to add. Contacts have the option to receive notifications via two different methods which are SMS or Email.
A Contact can request to be sent notification via either or both methods.
The piece of the problem that I am stuck with, is that a Contact may wish to receive notifications differently depending on the specific distribution list.
So we have a problem like this :--
CONTACT A is in DL-A - Receives Notification via SMS
CONTACT A is in DL-B - Recieves Notification via Email & SMS.
I'm trying to avoid having more than one entry for a Contact in my Contacts Table, each contact should be unique.
Can Anyone help?
You could use another junction table:
contactid, distributionlistid, messagepreference
Messagepreference can be email or SMS. Two rows if they want both. New messaging types can be added with no changes to the DB. To be safe, use constants in your code to represent the values you will put into the columns.
Or, add sendemail and sendsms columns to the original junction table, but this has the drawback that you have to change DB structure if you introduce a new messaging type.
So you can add to fields in the junction table:
ContactsDistributions(ContactId, DistributionId, SMSFlag, EmailFlag)
in order to specify the type of notification choosen by the contact for each distribution.
You can add one more field in junction table which will represent how given contact will receive notification from given distribution list.
In this case I would add two fields to the junction table SMS and email both boolean and set to true if and only if they wish to receive notification in that matter. This allows the notifications to be set differently for combination list and contact.
Also depending on how you want to deal with removal from lists you could add a constraint on the junction table so that at least one of the two fields is true so that a notification is always sent although say in google groups I do have access to some lists which I have chosen not to get notifications from.

MySQL joins for friend feed

I'm currently logging all actions of users and want to display their actions for the people following them to see - kind of like Facebook does it for friends.
I'm logging all these actions in a table with the following structure:
id - PK
userid - id of the user whose action gets logged
actiondate - when the action happened
actiontypeid - id of the type of action (actiontypes stored in a different table - i.e. following other users, writing on people's profiles, creating new content, commenting on existing content, etc.)
objectid - id of the object they just created (i.e. comment id)
onobjectid - id of the object they did the action to (i.e. id of the content that they commented on)
Now the problem is there are several types of actions that get logged (actiontypeid).
What would be the best way of retrieving the data to display to the user?
The easiest way out would be gabbing the people the user follows dataset and then just go from there and grab all other info from the other tables (i.e. the names of the users the people you're following just started following, names of the user profiles they wrote on, etc.). This however would create a a huge amount of small queries and trips to the database in a while loop. Not a good idea.
I could use joins to retrieve everything in one massive data set, but how would I know where to grab the data from in just one query? - there's different types of actions that require me to look into several different tables to retrieve data, based on the actiontypeid...
i.e. To get User X is now following User Y I'd have to get my data (User Y's username) from the followers table, whereas User X commented on content Y would need me to look in the content table to get the content's title and URL.
Any tips are welcome, thanks!
Consider creating several views for different actiontypeids. Union them to have one full history.