Laravel add Auth session variables - php

After user logging to the system(Auth::attempt success)
How to add additional data to the Auth session for user ?
I have user all data but there is no profile image data of user.Profile image of user is save into another table like user_profile_image.
I have to set profile image data in Auth::user()->file_name till when user logout.
I tried this way:
Auth::user()->setAttribute('key','value');
and
Auth::user()->file_name=$filename;
but it doesn't work.

If you want to just save the user picture:
add a migration that adds the picture column in the users table
Use the field to save the picture
$u = User::find($userId);
$u->picture = $pathToPicture;
$u->save();
then access it with
$u->picture;
or
Auth::user()->picture;
if you want to add fields to registration form: http://www.easylaravelbook.com/blog/2015/09/25/adding-custom-fields-to-a-laravel-5-registration-form/

Related

How to store user data once user login with Facebook/Google account on my website

I have a website where I store user info like user id, email , password, and some other info once user registered, where user Id is auto generated field in DB with Auto Increment method in MySQL.
Now I want to implement Facebook & Google Login. Facebook & Google gives very long user id(20 to 36 char long) once we make the request to there api but I can store max 6 char long user Id.
So I want to know what is the best way to store it. Shall I create a new table where I can create mapping with FB/G+ user id to my User's table user id and every time user login I check this mapping table and get user id created by my DB corresponding to FB/G+ user id and perform all the operation using my user Id OR shall I store FB/G+ user id directly to my User table but for this we need to change Table structure and many table because user id is FK for many tables

pass collection during redirect to route in laravel

I have a form in a view where a user can add an item to the DB.
After successful insert in the DB I want to redirect to a view that has latest entries from other users but I want to send along the item that the user just added.
I mean to save a DB query in that view controller where then I should only retrieve for example the latest 10 entries from other users less the one that the auth user just added.
Is it possible?
Looks like you want to display recently added record separately. In this case, you can pass data to the view after inserting it to DB. Something like:
$otherUsersLastItems = Model::latest()->take(10)->get();
$lastItem = Model::create($request->all());
return view('some.view', compact('otherUsersLastItems', 'lastItem'));

ajax save in database without having foreign key

I have a form for users to sign up. This form has a file input which will upload images in a directory in server by ajax instantly as user selects a file. There's also a button which will submit the form and saves user info in user table in database.
Each user can have multiple images. Ajax also save address of images in photos table when file uploads. this table has user_id column. but since the user_id will be generated when submit button is hit and user is not registered yet, I can't fill user_id column in photos table. The id of user is generated when whole form is submitted.
so should I update photos table when form submits by adding id of user to user_id column?
or I must save photos with ajax in temporary_photos table and then when submit is hit save them in main photos table and delete them from temporary table?
or is there another approach?
what if user uploads files, but closes the window without hitting submit button? how I find out the window is close and registration is canceled so i will delete images?
You are trying to solve a problem that you created. If user_id is a dependency for the photo to be saved, let the user be saved first.If you do it the other way , it you will over complicate the workflow.
So, i suggest you register the user first, save the user id in session (or log the user in) and then upload the photo.
If you must do it together, then let the ajax request register the user and then save the photo and save the user_id in a session variable. Next time you upload a photo you could get the user from session variable and save the photo.
function savePhoto(Request $input){
if(Session::has('user_id'){
$user = User::find(Session::get('user_id'));
}
else{
$user = User::create($input->all());
}
$user->photo->save($input->all());
}
You can try this way-
Since an user can have multiple images that means the relation is one to many between. So better if you use a images table.
Table: Images
Column(s): id, user_id, image, uploaded_at
Make user_id field is nullable and image column will save the name of uploaded images. Upon successful uploading and adding iamges record to database save last inserted ids in a session array. Once the user submit the form, create a record to user table, grab the last insert it, check if there are any data in session array, if found, loop through the array and select those images record by id and update user_id field. This will solve your problem.
You also asked for a way to delete images for which users didn't submit the form. To do this, you can run a cronjob/laravel task schedular. A script that will remove images and delete records for those images which were uploaded 5 mins (for example) ago and user_id is still null.

session data updating and retriving in yii

firstly I have two tables
1) family - stores data about family like familyid,city,homephone,address,email
etc
2) person - stores about individuals like
persontype,gender,mobile,email,firstname,lastname etc
these tables are filled while signup.
After signup,
Whenever user logins all the data from person and family
retrieved and stored in session and used anywhere in the website by session.
$person=new Person();
$account=Onlineaccount::model()->find(array('select'=>'PersonId,EmailAddress,LastLoginDate,FailedLoginCount', 'condition'=>'EmailAddress=:EmailAddress','params'=>array(':EmailAddress'=>$model->username),));
$person=new Person();
$person=Person::model()->find(array('select'=>'PersonID,FamilyId,FirstName,LastName,MiddleInitial,
MobilePhone,PrimayContact,PersonTypeCode,DateOfBirth,GenderCode,EmailAddress', 'condition'=>'PersonID=:PersonID','params'=>array(':PersonID'=>$account->PersonId),));
$familyDetails=Family::model()->find(array('select'=>'FamilyID,HomeAddress1,HomeAddress2,HomeAddress3,City,State,Zipcode,HomePhone,HomeFlag,CurrChapterCode,FinanceMasterRecID,Membersince',
'condition'=>'FamilyID=:FamilyID','params'=>array(':FamilyID'=>$person->FamilyId),));
// $session=new CHttpSession;
// $session->open();
$_SESSION['newaccount'] = serialize($account);
$familyDetails->firstName= $person->FirstName;
$familyDetails->middleName= $person->MiddleInitial;
$familyDetails->lastName= $person->LastName;
$familyDetails->gender= $person->GenderCode;
$familyDetails->dob= $person->DateOfBirth;
$familyDetails->email= $person->EmailAddress;
$familyDetails->userName= $account->EmailAddress;
$familyDetails->mobilePhone= $person->MobilePhone;
$familyDetails->registrantType= $person->PersonTypeCode;
$familyDetails->PersonID=$person->PersonID;
$familyDetails=$this->getMembershipDetails($person->FamilyId,$familyDetails);
$_SESSION['familyDetails'] = serialize($familyDetails);
Now the question is ,When user logins and in his profile he wanna change the field like lastname through update form...the lastname is updating but the updated data is not showing after refreshing also it is displaying last data because of sessions...when user logout and login the newly updated data is showing..
how to show the updated data after refresh through session in yii ..
thanks for reading and waiting for suggestions..please help me..
After updating the profile reassign the session values again. Then only you can access updated data every where in the application.

Mysql and Sessions , PHP

I currently have a website that users log in and uses sessions.
The currently login and use the " users " table and it directs them to the appropriate page. I have another table that stores other details about the user.
I wish to display information to them from this other table, but they are controlled by their username in a session, Can I link the second table?
You can add a column on your table to set who is allowed to view that link by adding a user type definition to your table of users.
user_privilege = set(admin, customer, stupid)
Then check user privileges

Categories