customized user id in User table in laravel - eloquent

Helo I am trying to assign a user id with some prefixes like PS00 and save it in user table while user registration.
I have tried mutators in laravel
public function setProfileIdAttribute($value){
$this->attributes['profile_id'] = "PS00".$this->id;
}
But unable to add profile_id to table, as it says profile_id can not be null.

Related

Adding data to Postgres database with Entity Framework Core HttpPost failing because of child table constraint

I am trying to add some data to a Postgres database using Entity Framework Core via a Http POST method.
The data to be added is passed in the body of my post request which looks like this:
{"id":42,"name":"Hans Musterman","email":"hans#gpost.com", "gender": {"id": 2, "name": "male"}}
Which is exactly the structure I would get returned using a get request.
Still the insert fails with an error:
Duplicate key value violates unique constraint "PK_Genders"
Of course the gender male already exists in the Genders table. What I want to do is add a user to the users table with a gender but referenced for the new user but not new created. What the system seems to do when using DbContext Add is trying to add User and Gender.
Is there a way to do it with a reference? Adding a User with "gender" = null does work.
I think you should have something like genderId or foreign key that reference the gender table in the user model use that foreign key id (genderId) instead of gender.
eg. in you user object
{ ... "genderId": 2 }
Otherwise Entity Framework is going to create a new gender that is why you are getting that error

Geting user details using foreign key from res.users in Odoo

I have a model Employee and have included a foreign key to store the user object from the res.users table using the following code in my model:
user_id = fields.Many2one('res.users', string='user id', default=lambda self: self.env.user)
In my form, I am already capturing the user object.
Now, I want to display the user object's details (id, name, email) using the foreign key (that is the user object) in my form view.
The <field name="user_id"/> in my form view would display the user name of the stored object. I want to display different fields for id, email.
you can create a related field for user_id,
x = fields.Char(related='user_id.email')
like this you can access the all field values of related record.

Eloquent primary key default value null on model save

I am using UUIDs as my primary key in laravel.
The UUID is not generated by eloquent but by a default value in postgresql.
The default value is uuid_generate_v1mc(). This function is from the uuid-ossp package. The id is correctly generated.
But now the problem.
For example, I am creating an user:
$user = User::create(['email' => 'adsf', 'name' => 'adsf', 'password' => 'adf'])
This returns an user model without an id. I've found that I had to do an fresh() on the model to get the default values. But when I do this I get null. I think this is because the fresh function needs the id. But doesn't have it.
The user model has public $incrementing = false;. So that is setup correctly.
How can I get the full user model(In a clean way) when I create it? For the user the email is unique, so I could get the user based on the name. But for other tables this might not work.

Laravel Foreign Key 'Trying to get property of non-object'

I simply cannot wrap my head around Models.
I have two tables, users and companies. Users contains a column called Company which is a foreign key to the companies table. the companies table has two columns, an ID and company_name The value of Company under users references the id in Companies.
I am trying to get the value of company_name through the foreign key like so.
$user = User::find(1)->company->company_name;
My thought process, which may be wrong, is that this (depending on if models are correct) should get the value of the Company column and then using that value, get the company_name from the Companies table.
My models look like so
User
public function company() {
return $this->belongsTo('App\Company', 'company');
}
Company
protected $table = 'companies';
public function user() {
return $this->hasOne('App\User');
}
All I continuously get is Trying to get property of non-object
I can see in the error log it gives that it's actually getting the company name too!
Trying to get property of non-object', 'C:\wamp\www\laravel\app\Http\Controllers\HomeController.php', '41', array('userid' => '1', 'usercompany' => '1', 'company' => 'BMW'))
But I don't understand where it's pullyig userid and usercompany from.
What am I doing wrong?
In this case, you're trying to get the property of a non-object, meaning that either User::find(1) or company is not an object. In the error message provided, it looks like one of the two is an array.
Look at your underlying database tables. Generally, the id is a protected field that is not returned in the result object. usercompany looks like the concatenated field to cross-reference the two tables (i.e. the foreign key).

Extend User authentication object in Azure Mobile Services

Is it possible to add additional properties to the User object on the server in WAMS? I would like to store the Id primary key of my User table for (secure) use in my table scripts. At the moment the only id is the vendor specific authentication Id, but I'd like to be able to allow users to choose an authentication method. Currently my (simplified) table design is as follows:
User table:
id
googleId
twitterId
facebookId
name, etc...
League table
id
userId
name, etc
I'd like to store the user primary key in the userId field on the league table, and then query it to ensure that users only get to see leagues they created. At the moment, the user object in table scripts sends through a User object with the Google/Twitter/Windows authentication token and I have to do a query to get the primary key userID, everytime I want to carry out an operation on a table with a userId column.
Ideal solution would be that when the Insert script on my User table is called on registrations and logins I can do:
// PSEUDO CODE
function insert(item, user, request) {
var appUserId;
Query the user table using the user.userId Google/Twitter/Facebook id
If user exists {
// Set a persisted appUserId to use in all subsequent table scripts.
user.appUserId = results.id;
} else {
Set the GooTwitFace columns on the user table, from user.userId
insert the user then get the inserted record id
// Set a persisted appUserId to use in all subsequent table scripts
user.appUserId = insertUserPK;
}
}
Then, in subsequent table scripts, I'd like to use user.appUserId in queries
If all you are trying to do is authorize users to only have access to their own data, I'm not sure you even need the "user" table. Just use the provider-specific userId on the user object to query your "league" table (making sure the userId column is indexed). The values will be provider-specific, but that shouldn't make any difference.
If you are trying to maintain a notion of a single user identity across the user's Google/Facebook/Twitter logins, that's a more complicated problem where you would need a "user" table and the kind of lookup you are describing. We hope to ship support for this scenario as a feature out of the box. It is possible (but fairly messy) to do this yourself, let me know if that's what you're trying to do.