you are doing way to much. It is quite simple actually.
I am assuming
$pbUserTable = User::where('email', '=', $pbloginEmail)->first();
$pbUserTable is the User Object of the logged in user.
say you have a column on your user table called login_number
All you would have to do is, upon successful login, run the following
$pbUserTable->increment('login_number');
will automatically add +1 to whatever the content is of that column. On your DB schema set a default of 0 on that column.
Thanks @Kryptonit3 for the support but i added your solution to my code. i still got some error.
This is my code for the controller.
public function login(){
$pblogin = Input::all(); // Unique value for validation
$pbloginEmail = Input::get('email');
$pbloginPassword = Input::get('password');
// check if user has confirmed his/her email...
$pbuser = User::where('email', '=', $pbloginEmail)->first();
$pbuserid = $pbuser->id;
//echo "User id". $pbuserid;
if (Auth::attempt(array('email' => $pbloginEmail, 'password' => $pbloginPassword)))
{
$pbUserTable = User::where('email', '=', $pbloginEmail)->first();
$pb_user_name = $pbUserTable->name;
$pb_user_name_saved = Session::put('pb_user_name',$pb_user_name);
$pb_user_email_saved = Session::put('pb_user_email',$pbloginEmail);
//save the incremental number of logins.
$number_of_login = new Numberoflogins;
$per_user_login = Numberoflogins::where('user_id', '=', $pbuserid)->count();
$existingRow = Numberoflogins::where('user_id', '=', $pbuserid);
$login_times = $number_of_login->no_of_logins;
echo "login_times" . "=" . $login_times;
if ( $per_user_login == 0 ) {
// insert data...
$login_times = $number_of_login->no_of_logins;
$number_of_logins = new Numberoflogins;
$number_of_logins->user_id = $pbuserid;
$number_of_logins->increment('no_of_logins');
$number_of_logins->save();
}else{
// update data...
/*$number_of_logins = $existingRow;
$number_of_logins->user_id = $pbuserid;
$number_of_logins->no_of_logins = $login_times + 1;*/
$Member = User::where('email', '=', $pbloginEmail)->first();
$Member->user_id = $pbuserid;
$Member->increment('no_of_logins'); // your sollution
$Member->save();
}
Session::put('session_email', $pbloginEmail);
//return Redirect::intended('dashboard');
}else{
return Redirect::to('/login')-> with('message', 'Your username or password combination was incorrect');
}
}
The error it produced.
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'no_of_logins' in 'field list' (SQL: update `users` set `no_of_logins` = `no_of_logins` + 1, `updated_at` = 2015-02-19 17:31:35 where `id` = 5)
Please help @Kryptonit3
what table is the login count stored in? and what is it's column name?
@Kryptonit3 Table: number_of_logins with three colums id, user_id, no_of_logins with laravel timestamps.
you can delete that table and relation completely. All you need to do is add a column to the user(users) table called no_of_logins with a default of 0. Based on your code you want to check if user has confirmed email (account) so I would recommend adding a confirmed column to your user(users) table as well with a default of 0.
$table->integer('no_of_logins')->default(0);
$table->boolean('confirmed')->default(0);
Then your authentication code should look like this
public function login(){
$rules = [
'email' => 'required|exists:users',
'password' => 'required'
];
$input = Input::only('email', 'password');
$validator = Validator::make($input, $rules);
if($validator->fails())
{
return Redirect::back()->withInput()->withErrors($validator);
}
$credentials = [
'email' => Input::get('email'),
'password' => Input::get('password')
];
// check if user is authentic
$valid = Auth::validate($credentials);
if ( ! $valid)
{
return Redirect::back()
->withInput()
->withErrors([
'message' => 'We were unable to sign you in. Incorrect email/password combination!'
]);
}
// user is valid, lets check a few things
$user = User::where('email', '=', Input::get('email'))->first();
$isConfirmed = $user->confirmed;
// check if user has confirmed their account
if ( ! $isConfirmed )
{
return Redirect::back()
->withInput()
->withErrors([
'message' => 'You must confirm your account before you can use our site!'
]);
}
// it appears as though this user has provided the correct
// email and password combination and their account has
// been confirmed so let us increase their no_of_logins count
// and login the user
// +1
$user->increment('no_of_logins');
// Store your session variables
Session::put('pb_user_name',$user->name);
Session::put('pb_user_email',Input::get('email'));
// incase you decide to use 'Remember me?' checkbox on login
$remember = Input::get('remember');
// login the user
Auth::login($user,$remember);
// redirect to the page they were trying to view, or redirect to index
return Redirect::intended('/');
// Use this if you want to redirect to a named route instead
// return Redirect::intended(route('home'));
}
here is a good email verification tutorial you can implement to make users verify their emails before being able to use your site
http://bensmith.io/email-verification-with-laravel
P.S. - you will then be able to access the users number of logins two ways.
$user = User::find($userId);
$no_of_logins = $user->no_of_logins;
$no_of_logins = Auth::user()->no_of_logins;
Thanks @Kryptonit3 for the support in relation to the sollution you posted, i injected your code (login function) to my code. it is working but it depends on another controller EmailConfirmationController to set the $user->confirmed = 1; so that the login can check if it is confirmed from your code. but the issue i have is that it is not setting the confirmed column to 1. so that the user can be logged in. This is my code
class EmailConfirmationController extends \BaseController {
/**
* Registration manages the confirmation link sent to the users
* who registered for the first time.
* ------------------------------------------------------------
*
* @method confirm string
*
*/
public function confirm($confirmation_code)
{
/**
* Check for confirmation link with code in the database
* if true set the confirmation code field to null and
* the confirmed feild to 1 then redirect to login page
* else redirect them to login with error message.
*/
if( $confirmed_code = Emailconfirmation::where('confirmation_code', '=', $confirmation_code)->first() ){
$user_id = $confirmed_code->user_id;
$confirmed_code->user_id = $user_id;
$confirmed_code->confirmed = 1;
$confirmed_code->confirmation_code = null;
$confirmed_code->save();
// because of Kryptonit3 (laravel.io/forum) sollution
// i will add a one value to the user::confirmed column.
$user = User::find($user_id);
$username = $user->name;
$useremail = $user->email;
$userpassword = $user->password;
$userlogintimes = $user->no_of_logins;
//////////////////////////////////////////
$user->name = $username;
$user->email = $useremail;
$user->password = $userpassword;
$user->no_of_logins = 0;
$user->confirmed = 1;
$user->save();
return Redirect::to('login')->with(array('welcome_back' => 'You have been confirmed please login .'));
//return Redirect::to('login')->with(array('confirmed'=>'true','send_to'=>'chooseaccount'));
}else{
return Redirect::to('login')->with(array('message' => 'The verification link is expired or not correct. <a href="http://popibay.com">Register</a>'));
}
}
}
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community