Laravel get a random user with where clause - sql

My table schema is a id field, name field and a friends id field.
Every user must have a maximum of 2 friends. When a new user is created and event is fired and a listener which listens to the created user event then adds a random friend to a newly created user.
$randomfriend = DB::table('users')->select('id')
->groupBy('friends_id')
->havingRAW('COUNT(*) < 2')
->inRandomOrder()->first();
it still returns users with maximum number of friends. Can someone help me with this?

first of all create another table for friends relations.
for selecting a random user this post can help you:
enter link description here
don't forget to put this code in while loop for checks.

Related

How do I check user by ID or Name?

I want to check if the message what I got comes form the particular user and I want to check him by his Name or ID, because for some users I don't know the Id yet. Is there a good way to do it?

How to provide ID counter starting from 1 for each new user?

I am implementing a multi-users website. They can create "items".
When a new user registers and creates his first item I want it to have an ID of 1, not 2728 (its ID from DB).
So I need some kind of mapping? E.g.
| real_item_id | fake_item_id |
2728 1
So for each Account I will have a table with fake_id starting from 1 and incrementing every time a User creates an item?
Maybe there is another, better approach?
I think you don't need any new table for this. Just add a column fake_id into items table and scope it with user_id for uniqueness.
Also create a custom validation method in model to assign fake_id to Item while creating. Thanks
Simply create a table to track items created by each User and keep on increasing Items count whenever user creates new item and update increased items id into main table(more easier)
or
Another approched will be keep two column on your table one User ID and another one for Items Id which will get increamented id on each new item inserted by particular user by checking last item inserted by particular user.

MS Access 2007 - Select multiple records and assign a value into a field

I am using a Multiple Items Form to list CASES (records) where there is no TECHNICIAN assigned (Maybe I should use a Datasheet to list the records?).
I would like the user to select a TECHNICIAN from a dropdown field that gets its values from an Employee Table (I can do this). Then I would like the user to select multiple CASES (records) in order to assign that one TECHNICIAN to the Technician field in all of the selected CASES.
Basically, I'm trying to keep the user from having to assign a technician from within each and every incoming case request. I want them to "batch" assign a tech to multiple cases.
Can someone point me in the right direction?
Ok so I did some more research. This may not be the best answer but it works for now.
I created a Multiple Item Form.
I added an unbound dropbox that lists Employees from the table
I added a button on the detail section (for each record) with the follow line of code:
Me.Technician = Me.Choose_Technician
Now the user can pick a technician from the dropdown and then click the button to assign that technician to the record/casefile.
This is a simple solution if you only have a couple of records/casefiles to assign. If the amount of incoming casefiles increases there will have to be a way to select multiple records using the shift key. I'll keep researching this.

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.

How should I handle entries when the user is deleted?

I run a forum which I built myself. Ok so all users have the opportunity to delete themselves.
But all their threads and posts will remain. But right now where it should say their username it's just blank.
How should I handle this?
Should I make a new user and call it e.g. "deleted user" and assign all threads/posts to that ID when they delete themselves?
Or should I just check if the user ID exist if not print e.g. "deleted user" as username?
What's the smartest way? Any other ways tell me.
Thanks!
p.s (i'm not a native english speaker, looked up some fancy words on a online dictionary)
I would suggest not actually deleting the user. You could simply add a column to the users table such as:
ALTER TABLE users ADD COLUMN (is_active TINYINT(1) NOT NULL DEFAULT 1);
Then when you "delete" a user, simply mark them as inactive:
UPDATE users SET is_active = 0 WHERE users.id = 7;
For user listings, and account access you would check the is_active status. For displaying of data such as posts and what not, you'd not care about their active status, youd just grab the name from the table.