how to insert skills for users with laravel - laravel

I have a (skills) table and (users) table, and I have created (skill_user) table as a pivot,
but my problem is how can I let the user insert any skills he wants, and at the same time it is many to many relationships, which means the user should select the skills which already exist? i cannot understand how can I fix this issue?

There are 3 methods to work with many-to-many relationships:
attach
detach
sync.
I would say sync is more suitable for your case: user selects a list of skills, and you save only these. This also allows to remove skills if user unselected one.

Related

Laravel models, database and pivot tables question

Hello I am working with Laravel,
I have to create two simple models, let's say Stores and Books.
Stores can have one or multiple Books and Books can belong to many Stores.
Of course I will use a many to many relationship, with a pivot table.
Books the can have different prices depending the store.
I think a separate table can only complicate things, in my mind the pivot table associating books and stores should have a price column, but pivot tables only contains store_id and book_id.
Should I create a book_prices and associate it with books and to stores? What is the best approach?
You are free and able to set other attributes on your pivot table. You can read more about it in the docs.
https://laravel.com/docs/9.x/eloquent-relationships#retrieving-intermediate-table-columns
You have to define the relationship accordingly, the following should clarify how this works. In this example you use the many-to-many relationship and add the price column to every retrieved pivot model.
public function books()
{
return $this->belongsToMany(Book::class)
->withPivot('price')
}
For example, you are able to access the pivot column in a loop like this
foreach ($shop->books as $book)
{
echo $book->pivot->price;
}
You can define additional columns for your pivot table in the migration for the pivot table, and then when defining the relationship use withPivot to define the additional columns so they come through in the model:
return $this->belongsToMany(Book::class)->withPivot('price');
(Adapted from the Laravel documentation, see https://laravel.com/docs/9.x/eloquent-relationships#retrieving-intermediate-table-columns)
Depends on the complexity of your case, but yes, you have two options for it. Let's say that the pivot table is called as book_store:
Directly adds price column to book_store. This is obviously the simpler option. The drawbacks are:
The history of the price changes isn't logged. You'll have to create another table for logging if you want to keep this history information.
Changes made to price will directly change the price of the related book_store record. Meaning that a price is being updated "live" e.g users cannot update the price now but "publish" it some time later just like this example in the doc.
Create a new, different table to store the price. This may seems relatively more complex, but it may also be more future-proof.
Basically, you get 2 things that you miss in the first option above.
Don't think too much about book_store being a pivot table. One way to see it is like this: book_store IS a pivot table from books and stores tables viewpoints, but it's also just a normal SQL table which could relate to any other tables using any kind of relationships.
If you want to implement this, make sure to create a primary-key in the book_store table.
Alast, it all depends on what you need. Feel free to ask if you need more insight about this. I hope this helps.

How can I create groups of users in a waiting room automatically?

We are trying to enable collaborative chats in an educational application. The general idea is that the users (students) will start an exercise, this will make them join an specific waiting room. And somehow the system should be able to decide it has enough students to create a group of n students (depending on the exercise) according to a given strategy and will send a message to those students to join a chatroom with a generated ID so that they can collaborate.
Right now we are totally blank and cannot decide on how to make the server decide wether to try and create groups or to wait for more students. Our stack is in Spring Boot, Redis and Postgress. Our initial idea was to add the students into a waiting room in Redis and launch a Spring event every time a student joined the waitlist. However, we understand that approach might generate many race conditions, which should be avoided.
Create an exercise_students table which has a SERIAL column on it, call it arrival_order or something. Another column for group_id. As students sign in for the exercise, insert them into this table. By nature SERIAL is atomically auto-incremented, so you avoid race conditions. Regularly query the table for students with no group_id (I assume you have a exercise_group table of some sort that defines how many students are part of a group). When the count reaches n, update them with the group_id and create a new group in the exercise_group table for the next group.
Relational databases are pretty good at this sort of thing. Atomic updating of state is pretty straightforward stuff.

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.

Achieve one to many relationship Spring MVC

I am trying to achieve one to many relationship. I know how to do basic one to many relationship between requestor id and userid.
My question is How to refer gtlUserId(resourceRequestTable) to gtlUserId (User table) as by default spring is mapping gtlUserId (resourceRequestTable) to userId in user table
It has some Ways.
I think you should give a specific way during all project !
As My experience each many to one must be a Drop Down in Client side .
However in your Table ResourceTypeEntity should be drop down inside ResourceRequesTable when value of option is Id[primary Key].
Also Your table not seems good design why two many to one map to same table? it may cause 3NF problem in DB also pay attention Cascade it when Parent Table related to other Parent is not good Design ,Keep it simple with uni Direction Many to One And force user to delete manually parent is better ,CaseCade Delete when Parent has related to other table will make exception handling and testing too hard.
please take a look https://examples.javacodegeeks.com/enterprise-java/spring/mvc/spring-mvc-dropdown-box-example/

Database schema for rewarding users for their activities

I would like to provide users with points when they do a certain thing. For example:
adding article
adding question
answering question
liking article
etc.
Some of them can have conditions like there are only points for first 3 articles a day, but I think I will handle this directly in my code base.
The problem is what would be a good database design to handle this? I think of 3 tables.
user_activities - in this table I will store event types (I use
laravel so it would probably be the event class name) and points for
specific event.
activity_user - pivot table between user_activities and users.
and of course users table
It is very simple so I am worrying that there are some conditions I haven't thought of, and it would come and bite me in the future.
I think you'll need a forth table that is simply "activities" that is simply a list of the kinds of activities to track. This will have an ID column, and then in your user_activities table include an 'activity_id' to link to that. You'll no doubt have unique information for each kind, for example an activities table may have columns like
ID : unique ID per laravel
ACTIVITY_CODE : short code to use as part of application/business logic
ACTIVITY_NAME : longer name that is for display name like "answered a question"
EVENT : what does the user have to do to trigger the activity award
POINT_VALUE: how many points for this event
etc
If you think that points may change in the future (eg. to encourage certain user activities) then you'll want to track the actual point awarded at the time in the user activities table, or some way to track what the points were at any one time.
While I'm suggesting fourth table, what you really need is more carefully worded list of features to be implemented before doing any design work. My example of allowing for points awarded to change over time is such a feature that you don't mention but you'll need to design for if this feature is needed.
Well I have found this https://laracasts.com/lessons/build-an-activity-feed-in-laravel as very good solution. Hope it helps someone :)

Resources