I am trying to represent friendships in my Laravel project database.
I have created a table profile which contains all user-data.
A second table profile_profile with the columns profile_id and friend_id.
With that I am able to get persons wo wants to be friend with me:
$this->belongsToMany('App\Profile', 'profile_profile', 'friend_id', 'friend_id');
an friends I am want to be befriendet:
$this->belongsToMany('App\Profile', 'profile_profile', 'profile_id', 'friend_id');
But in the end you are only friends if both want that so. So I tried to combined them but join or something similar didn't work.
I did a research on this platform and google over 2 hours. I am afraid that my skills are to limited, but I really like to ask for help.
Thank you in advance.
Quick solution: add another column in the profile_profile table, say, "accepted" as boolean and default to FALSE.
When a user accepts a friend's request, toggle the "accepted" field to TRUE.
Then you could search like $profile->profiles()->wherePivot('accepted', true)->get()
Related
I've Googled for a while and found multiple threads discussing problems like this, but I just can't get my head around how to do exactly what I want to do, hope someone can point me in the correct direction.
I'm making a learning platform in Laravel. What I want right now is that when lesson A is completed by someone belonging to workplace B a notification should be sent to user C. So I have made a table notification_receivers containing lesson_id, workplace_id, and user_id, pointing to the respective tables.
Of course, I also have the corresponding models (User, Lesson, Workplace) set up, but what I can't understand is exactly how to set up the model relations. I'm currently making the Blade template used for editing the notification receivers belonging to a particular lesson, and I need to make the following: Get all notification receiver users for this lesson and then for every one of those users, get the related workplaces.
My first try was this in the Lesson model:
public function notification_receivers(): BelongsToMany
{
return $this->belongsToMany('App\User', 'notification_receivers')->withPivot(["workplace_id"]);
}
Which of course doesn't work straight off, since some users will be returned multiple times (once for each workplace). How do I do to get every user just once?
And when I have my users, how do I get the workplaces for each user? If I get it to work, the withPivot above will give me the IDs, but how do I get a collection of the Workplaces?
In the Parse js guide it says:
For example, a User may have many Posts that she might like. In this case, you can store the set of Posts that a User likes using relation. In order to add a Post to the “likes” list of the User, you can do:
var user = Parse.User.current();
var relation = user.relation("likes");
relation.add(post);
user.save();
My use case is actually this exact setup. We have Users and they can "like" 0-many Posts, so I set it up exactly as stated above. My issue now is that for a given Post or set of Posts, I need to query how many users have liked each Post. I'm having trouble figuring out how to query for that... Can someone help me out? I'm not sure if this is a limitation or if I just don't know enough to figure this out. I've thought about storing a counter on the Post itself and using increment() and decrement() each time a User likes or unlikes a Post, but I'd rather not if I can get away with it.
There are a couple of ways you can do this. One would be to also have a relation on each post for likedBy so when a user likes a post, you add that user to the post's likedBy relation as well as adding the post the user's likes relation.
Another solution is to create a new Parse class for Like. Whenever a User likes a post, you create a new Like with a pointer to the Post and the User who liked it. Then if you want to find the posts that a user likes, you can easily query that, or if you want to know which users like a post, that query is just as simple.
I personally use the second approach, but either will work
Ok so the basis is that I want to be able to have comments on each of my post. The comments will have a reply button for each. It’s not multi threaded so one comment will have many replies. Those replies won’t have replies. Also I want to be able to like and dislike a comment/reply. All of this will not be bind to the user model or any of such. The public visitors will be able to add comments and reply to comments but the approval is needed.
So here is the logic I got so far. Am I on the right track here (hoping this post may help someone else as well):
So i create aComment model. And then create a table named comments.
And I create a model named Reply and a table named replies
And finally, a model name Like and it’s table likes
So the relationship is:
comments will have many replies and replies belongs to one comment
replies & comments will have many likes.
And now for the logic:
I will use AJAX to call the store function on the CommentController to store comments. And I will call the store function on the ReplyController to store the replies. As for the likes, LikeController store function will store the likes for the comment and reply.
Here is the table structure:
Comments table
id
post_id
name
email
comment
approved
timestap
Replies table
id
comment_id
name
email
comment (or reply)
approved
timestamp
Likes table
id
comment_id
reply_id
like
dislike
timestamp
Now what I do not understand is, likes table. Is it right to have comment_id and reply_id and also like and dislike?
I could call the store function everytime someone clicks the like or dislike and have it stored in the table and update the column if it is a reply or a comment by it’s id to the respective id columns. Is the logic right?
Also, if you guys have any suggestions or better and efficient way of doing this, please let me know.
This is getting too long so I’ll just leave it here.
Edit
Also forgot to mention that I am not sure how I will be taking the amount of likes from db to blade and count it. Not with the current structure mentioned above. Also, how to check and see if the person already liked. And if so, don’t let them like again or dislike again. A liked person can not dislike. They can do only one thing.
I would definitely recommend associating comments, replied and likes to a User which you are pretty much already doing with comments and replies.
There are ways that you could have a "liking" system that would allow guest usage, however, this would be very easy to get around and ultimately make your "like" stats useless.
An example DB structure going forward would be:
comments and replies
id
post_id // or comment_id if on the replies table
user_id
body
approved
( "_at" dates)
likes
id
likeable_id // id of the comment or reply
likeable_type // Comment or Reply
user_id
liked (boolean)
("_at" dates)
The likes table is setup to be used as a Polymorphic relationship e.g. in your Comment model:
public function likes()
{
return $this->morphMany(Like::class, 'likeable');
}
Then to add a like you would have something like:
$like = new Like(['user_id' => auth()->user()->id, 'liked' => true]);
$comment->likes()->save();
There are many different ways you would then check if the current auth user has liked a post, one example would be to have a separate relationship so that you can eager load the results:
public function authUserLike()
{
return $this->morphOne(Like::class, 'likeable')->where('user_id', auth()->id());
}
Then if the auth_user_like is null they haven't already liked that comment.
I don't see any need of the Replies Table just use parent_id in comments table. This tutorial might help you get started Nesting Comments in Laravel
I have a search database of car models: "Nissan Gtr", "Huynday Elantra", "Honda Accord", etc...
Now I also have a user list and the types of cars they like
user1 likes: carId:1234, carId:5678 etc...
Given user 1 I would like to return all the cars he likes, it can be 0 to even hundreads.
What the best way to model this in Solr or potentially another "nosql" system that can help with this problem.
I'm using Solr but I have the opportunity to use another system if I can and if it makes sense.
EDIT:
Solr solution is to slow for Join (Maybe we can try nested). And the current MySQL solution which uses join tables has over 2 billion rows.
so, you just want to store a mapping between User->Cars, and retrieve the cars based on the user...sounds very simple:
Your docs are Users: contain id (indexed), etc fields
one of the field is 'carsliked', multivalued, which contains the set of car ids he likes
you have details about each care in a different collection for example.
given a user id, you retrieve the 'carsliked' field, and get the car details with a cross collection join
You could also use nested object to store each liked car (with all the info about it) inside each user, but is a bit more complex. As a plus, you don't need the join on the query.
Solr would allow you many more things, for example, given a car, which users do like it? Elasticsearch will work exactly the same way (and probably many other tools, given how simple your use case seems).
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