I am writing a web app where there are users with different account types and roles. The DB is set up in such a way where there is a Users table and then a Customers table which carries the related user_id.
I have set up the relationship with the Users Eloquent model and the Customers Eloquent model so the fetching of records works properly.
I was wondering however if there is a way within Laravel to apply the same logic for inserting or updating. For example, I am adding a new customer to the db. This requires adding certain data to the Users table and then certain data to the Customers table. Is there a smart way to do this in Laravel instead of adding the user data and then making sure the data was added before adding the customer as the relationship is already defined?
Thanks!
Related
I'm developing the CMS(College Management System) website using LARAVEL PHP framework. Now we need to build up to three types of consoles(Admin, Student, Employee). I'm stuck in the admin console that admin is able to perform the selections process. I have performed the task for the following.
Foreach series has many Users.
Foreach users have one Profile.
Foreach profile has many qualifications, experiences, attendances..
Students and Employees Record saved in the user's table.
Now we need to remove students and employees written in the eclipse shape because there is a duplication code in these two tables(student and employee), then we need to create the profiles table. Now remaining tables classes and courses(I have not yet decided what type of relation).
If you need any code from this example then I will add in the comments section.
In order to prevent duplication (if students and employees will always be the same entity) is to introduce a new model in the system called Person (or something similar). Then you will create additionally a PersonType enabling you to expand the model further if you ever need something more than student and employee.
DB schema would then have a relationship:
persons ---- M:1 ---- person_types
Where person_types would have:
ID Name
1 Student
2 Employee
This way you can reuse everything.
I have an application where a User can be a Supplier or a Shop.
Now these 2 roles have very different role-specific fields..
Basically, a Shop can make Orders to Suppliers.. So a Shop can make Orders and a Supplier will receive those Orders.
Where would I put these fields for good practice?
Add them all on the User class, with nullable types (doesn't seem right!)
Create a different class for each Role. So we have a Supplier class with a user_id and Shop class with a user_id, I would have to query these fields using $user->supplier->field or $user->shop->field...
Neither of these seems like thé way to go. Any idea's?
Thank you!
You don't have to stick with a single users table. You can have a Supplier model and a Shop model (with suppliers and shops tables).
Then in the config/auth.php file, you can set up a new auth provider using the Eloquent driver but with a different model. You can then assign those providers to any new guards that you create.
You can read more about authentication and authorisation here:
https://laravel.com/docs/5.4/authentication
https://laravel.com/docs/5.4/authorization
Edit
After it was mentioned in the comments about polymorphic relations, I think that would apply better for this type of relationship (multiple types of users, each with own set of fields).
Read more here:
https://laravel.com/docs/5.4/eloquent-relationships#polymorphic-relations
Can a user have multiple shops or supply multiples? Your second option seems fine.
User hasMany: supplier, shop
- id
- name
Supplier belongsTo, hasOne: user
- id
- user_id
- fields_supplier
Shop belongsTo, hasOne: user
- id
- user_id
- fields_shop
$user->supplier->fields_supplier;
$user->shop->fields_shop;
$supplier->user->name;
$shop->user->name;
--
If you had more info on how you wanted it to work, it would help.
"Supplier" is a role played by a "Party" ("Individual" or "Organization")
Your store may get a Catalog from a Supplier, and may Request Quotes from them.
Your store creates "Purchase Orders" and sends them to the Supplier, and they send you "Order Responses"
A Supplier is not a user. You could create "User"s for their employees though
Why not just use an existing ERP like Odoo?
I was working on making a group functionality for my website which uses a many to many relationship between groups and users.
My User model looks like this:
public function groups(){
return $this->belongsToMany('App\Group')->withPivot('role')->withTimestamps();
}
My Groups model looks like this:
public function users(){
return $this->belongsToMany('App\User')->withPivot('role')->withTimestamps();
}
So my third column has the name of role which is a string variable and is set to a default of "member" for members of my group and I set it to "admin" for the actual user who creates a new group. But I want the admin to have the option of making multiple members admins as well which would require me to check weather the current current user who sent the request is an admin or not. If he is, then I wanna be able to take his request of making a member an admin which would require me to update the role for that particular "member" to an "admin".
In the laravel documentation it only shows you how to attach and detach data in a pivot table and else where I have only seen methods of retrieving data from the first two columns but how can I do the same for additional columns and also be able to update it using the updateExistingPivot method?
You could access the column simply using pivot e.g :
$user->pivot->role
Take a look at Retrieving Intermediate Table Columns in documentation Eloquent Relationships.
Hope this helps.
I am building a real estate related website that real estate agents and investors can use to track properties submitted in the system and keep track of who is owed what profits. I have the following tables that I am having trouble figuring out how to setup the relationships for in my models using PHP ActiveRecord:
properties
id
primary_profit_sharing
secondary_profit_sharing
commission
referral_payment
users
id
name
email
payments
id
type (commission, referral_payment, etc.)
property_id
user_id
What is the proper way to setup these relationships using PHP ActiveRecord? I would like to be able to access the user information for each payment by something like $property->commission and $property->referral_payment but I can't figure out how to setup the relationships in the models to allow that.
I currently have payments belonging to users and properties, and users/payments have many payments. All the information I was is returned in the query, but not in an accessible ways. I have $property->users but no way of getting the user information for a particular payment type.
The only way I can think of to accomplish what I'm looking for is to setup a table for each payment type, but that doesn't seem like the best way to do it.
In the payments table, instead of using property_id and user_id, maybe you should use fk_object as the foreign key and do your joins using that field. fk_object represents the id of the user or the id of the property.
I'm using Codeigniter with Ion Auth for user management. I'd like to create two different user roles - one for volunteers and one for organizations. I know that I can use Ion Auth's groups for things like access control, but what would be a good way to add fields to the different user types (for instance - the volunteer user will have a 'languages spoken' field while the organization will have a 'mission' field)? Should I extend the Ion Auth class for each new user type and handle CRUD seperately for each type, or use the 'groups' field and the user id to reference the fields in another table? Any insight as to an approach to this common problem?
I would recommend just adding all the fields you need into the meta table and only updating the ones you need per user group. If you need multiple groups per user check out Ion Auth v2.
I had the same problem before, what I ended up doing was building relation tables to handle different groups with different fields. Then I modified the profile() code a bit, to join the additional relation table according to the user's group settings.
I would start off building a relational database.
example:
**volunteers table**
id
user_id
languages
**organizations table**
id
user_id
mission
Then depending on user group, join the table in profile() function.
Hope this helps!