Where to add Auth session variables when the user is logging in - laravel

I’d like to add session variable to Auth when the user logs in.
Where should that code be exactly?
Here’s my structure:
Group table (parent table):
Id
Currency
Client_type
User table (child table):
Id
Group_Id (FK)
Name
I’d like for the values of Currency and Client_type to be available globally in Auth.
Thanks, using Laravel 5.2

Auth::user() will have the logged in user object from which you should be able to access all its properties.
edit: if the currency is a property of a related model then you should have your relations set up, maybe something like this in the User model:
public function group()
{
$this->belongsTo(Group::class,'Group_Id');
}
and you access it then like so
Auth::user()->group->Currency;
BTW I'm not sure how the tables are related.

Related

How to get specific model profile after authorization?

Here are some user profiles like: DoctorModel, UserModel, ClinicModel.
Each has own set of fields in database.
How to add concrete model in global scope when user is authorized to be able get model fields across all application.
For exmaple if user authorized as clinic I want to get from this model field nameClinic everywhere.
Now by defaul I got UserModel form Auth::user()
IMO this is somewhat a wrong approach. You can maintain a UserModel for all types of Users and the details that might change can be held on other Models.
For example, the ClinicModel belongsTo relationship on UserModel and holds the details specific to the clinic over there.

Laravel get relation by custom value

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...

Laravel: Check if the field was filled in the route, using Trait and Scope

I have an api that I am implementing multi-tenant based on an additional column in the tables.
For this I created a trait that implements a
static::addGlobalScope() and static::creating().
I have a user_idXtenant_id table. In this table I store the accesses that the user has.
My Route looks like this:
Route::get('{tenant_id}/products', function ($tenant_id) {
return App\Products::where(['tenant_id' => $tenant_id])->get();
})->middleware('role_or_permission:admin|seller|get-products');
Route::get('products', function () {
return App\Products::get();
})->middleware('role_or_permission:admin|seller|get-products');
The idea is that in the first route it is verified if the user has access to tenant_id and if he has access, return only the products of that tenant_id (here Polices can be useful and solve the problem).
The second route, on the other hand, must return all products of all tenant_id that the user has access to. For this, I use static :: addGlobalScope() to set the tenants that the user has access to in this way:
$builder->whereIn('tenant_id', $tenants);
The question is how to check in GlobalScope if tenant_id was filled in the model. So, if it is present, I only check the permission and if it is not present I include the tenant_id that the user has access to.
I hope I have been able to adequately exemplify the problem.
Any help is most welcome.
Thank you!

Laravel auth in custom fields

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?

Laravel authentication and authorisation

In my database, there is two table members and services. in that table, there are 5 column
members
Username, Rank, Name, Password, and more
services
username user_services
I don't want my default login from AUTH controller from users table but I want login from my members table and authorize table from another table services
if user logins authentication from members table and authorize from services table
Tried everywhere but couldn't get any valuable answer.
Thank You.
To change de auth table
Edit app/config/auth.php to change the table.
'table' => 'members',
If you need to change field in app/Http/Controllers/Auth/LoginController.php
public function username()
{
return 'yourField';
}
And you can continuos using AuthController from Laravel

Resources