I have an existing laravel project, where i create role base authentication like (user & admin) and this table relation with multiple table where relation make with id (primary key). Now i want to change primary key from id to user_id.
protected $primaryKey = "user_id";
It's call in User model. User table's changed id to user_id. But after change this, i have face many problem because of relational database with others table. How can i solve this problem without any change of migration table?
Related
Model - User
id [999]
name [foo]
Model - Post (without User Foreign Key)
id [1]
unique_key [USR_00000999]
data [bar]
I would like to get all user with related posts (one to many) by using "custom key" value, is this possible with build in eloquent?
I only manage to looping using foreach one by one with
Post::query()
->where('unique_key', sprintf('USR_%08d', $user_id))
->count();
That is not built-into Laravel by default... if you want to know why it's because it's not a common thing that everyone does...
BUT, you can use scope query so you don't need to do the sprintf everytime...
Laravel Query Scopes Documentation
But I want to ask, why wouldn't you just add user_id on your post table and just have an Accessors on your post model for generating the unique_key? That would be much easier on my perspective...
Laravel Accessor Documentation
UPDATE: (See Comment)
Is it possible to have an empty user_id on Posts table?
then populate it when the user is created?
say you have a posts with key of USR_00000999... and you have a user with an id of 999... when you create that user you'll just have to update all posts with key of USR_00000999 to have a user_id of 999...
I have a Users table that can store 8 different types of users. Depending on the user type, I have some other tables with the specific data set for it. So, I would like to use the ID from the Users table as a foreign key and primary key at the same time for those tables. But I am new at Laravel and I don't know how to do that to fit the conventions or, if it is not possible, how to do that when defining my models and one to one relations. Can someone help me? Thanks!
EDIT: Sorry, I think my explanation is not clear enough. Let's say I have 4 tables: users, user_types, customer_organzations, partners, and customers. I would like to use ID, that is PK of users, as a foreign key and primary key for tables customer_organizations, partners and customers, that have different information since they are different type of users in my application but all them login against users table. I don't care user_types table. I think I know how to deal with it.
You haven't given us much in terms of your table structure, so assuming you have a simple one-to-one relationship between a users table and a user_types table, the following would create the required relationship.
In your user_types table migration
$table->foreignId('user_id')->constrained();
The above will add your foreign key to the table and create the relationship back to the users table (this is done 'magically' using conventions so naming is important).
Then in your eloquent models, add the relationships between the models:
User.php
public function type()
{
return $this->hasOne(UserType::class);
}
UserType.php
public function user()
{
return $this->belongsTo(User::class);
}
Here is my data model.
I need to make the username column in the USER table as a unique column. But it should be unique with the company.
For example, Company A can have a username as James and Company B also can have a username as James.
To achieve this, my opinion is to make a unique constraint with username and company_id.
How can I do this?
You need to change your database adding company_id to users. then add a unique index (company_id,username) to your table.
user
----
user_id (PK)
company_id (UNIQUE INDEX)
emplyee_id
email
username (UNIQUE INDEX)
password
BUT i think you do not need a many to many relation,
could be a good idea to change your database removing the many to many reaction in this way:
company
-------
company_id (PK)
company_name
user
----
user_id (PK)
company_id
emplyee_id
email
username
password
From the data model, it is clear that, a user can belong to multiple companies and a company can have multiple users. So adding company_id to USER table will cause USER table to explode with lots of redundant user data for each company instance. The USER_COMPANY entity exists specifically to avoid this.
It is not advisable to handle this requirement using data model changes. The best way to handle this requirement is through application code or triggers.
The least inefficient way to achieve this through data model changes will be, to add username column in user_company table and synchronise user and user_company table for the user_name column across all DMLs throughout the application. That will again require lots of application code.
So the best solution is to leave it to the application and not disturb a good data model.
Hope this helps.
How would i make a single request to insert in multiple tables using laravel Eloquent ORM relationship .ie
Table 1 : users
id
name
email
Table 2 : posts
id
user_id
content
Table 3 : Image
id
user_id
post_id
image_name
relationship
Table users id references user_id in other two tables .
Table posts has One To Many relation with users.
Table images has One To Many relation with users and post ie it can be shared with other users and on other posts.
So that when one makes a post insert ,it should insert the record in the tables with a single query.
This is one way of doing it:
$post = (new Post)->fill($request->all()->toArray())->user()->associate(Auth::user())->save();
As for the image, the Post model should have a model event such as static::created to handle the image upload and manipulation.
Or to make more sense, a model event in the Post model should trigger another model event from the Image model.
->toArray() may be optional, I can't test it here where I'm now.
When you create model in laravel for eloquent, the User model is treated like a users table. Why is that? Can we use an exact table name for the model?
I think more than anything it's convention. I don't see why you couldn't create a Users model for your users table, but a User is an instance of users, which is why it's done that way. You can always specify the table the model uses with:
protected $table = 'name_of_table';
which can be different than the model name. For example, a Data model can use the table userdata, as long as you specify that.
Hope that helps.