Laravel Auth Issue while using different table name - php

This Questions is the continuation of this
I tried with the default user table for Laravel Auth purpose,
Now i am changing it to the admin Table
So i refereed here
Accordingly i tried to change the Table name as admin in the auth.php
'model' => 'User',
'table' => 'users',
to
'model' => 'AdminModel',
'table' => 'admin',
And in the AdminModel i have protected $table = 'admin';
Here is my AdminModel :
<?php
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
class AdminModel extends Eloquent implements UserInterface, RemindableInterface
{
protected $table = 'admin';
protected $fillable = array('UserName', 'Password');
public $timestamps = true;
public static $rules = array();
}
Here is my Controller :
if (Auth::attempt(array('UserName' => $UserName, 'password' => $password)))
{
return Redirect::intended('dashboard');
}
else
{
$queries = DB::getQueryLog();
print_r(end($queries));
}
And it is always printing the query like this
Array ( [query] => select * from `admin` where `UserName` = ? limit 1 [bindings] => Array ( [0] => a ) [time] => 1 )
What might be the issue ?

Related

Laravel Auth Attempt not working with Custom User Model

I generated a different table to store users for my website. Name of the table is tblusers. I am registering new users with a controller method register(), in which i added this code
public function register(){
return User::create([
'User_Email' => 'test#example.com',
'User_UserName' => 'test#example.com',
'User_Password' => bcrypt('123'),
'User_Address' => 'ABCD....',
'User_IsActive' => 1,
'User_FullName' => 'Burhan Ahmed',
'User_AppID' => 1,
'User_IsVerified' => 1
]);
}
It adds above dummy data successfully in Database. Then i tried to login with above given credentials using below code:
dd(Auth::attempt(['User_UserName' => 'test#example.com', 'User_Password' => '123']));
But above statement always returns false, Why? Am i missing something. I tried to pass actual bcrypt code instead '123' in above array it returns the same result always. Below is my Model Class
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Facades\DB;
//class User extends Authenticatable
class User extends Authenticatable
{
use Notifiable;
protected $table = 'tblusers';
protected $primaryKey = 'User_ID';
public $timestamps = false;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'User_UserName', 'User_Email', 'User_Password', 'User_Address', 'User_FullName', 'User_IsActive', 'User_IsVerified'
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'User_Password'
];
}
I am using Laravel 5.4, i followed all the authentication steps but not matter what i pass it always return false.
if You want to Change the default table of login folow the steps
For Example You are Changing it to login_table
Step1:
change the table property in User.php (User Model)
/**
* The table associated with the model.
*
* #var string
*/
protected $table = 'login_table';
Step1:
IF YOU ARE BEGGINER
Now You need to change the table name users to login_table
IF PROJECT IS TEAM COLLBRATION MAKE THE MIGRATION WITH login_table
php artisan make:migration create_login_table_table
and add the columns available in the users table
Step3:
Now open the file app\Http\Controllers\Auth\RegisterController.php
You will find method validator as
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
]);
}
Now You need to change unique:users to unique:login_table
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:login_table',
'password' => 'required|string|min:6|confirmed',
]);
}
Hope it helps and it works fine for me # Md.Sukel Ali
Comment if it not works

Saving to relational table Laravel 4

Basically I have a groups table with id and name,
I have another table with users_groups with id, group_id and user_id
I'm try to save when a user is created to the users_groups table, but when I use this code it saves what it can in the groups table. Is there something I'm doing wrong?
Account controller (saving user to db and set group to id 1)
$user = User::create(array(
'firstName' => $firstName,
'lastName' => $lastName,
'dateOfBirth' => $dateOfBirth,
'gender' => $gender,
'email' => $email,
'username' => $username,
'password' => Hash::make($password),
'code' => $code,
'active' => 0
));
Group::create([
'user_id' => $user->id,
'group_id' => '1' //1 is not assigned
]);
Group Model
class Group extends Eloquent
{
protected $fillable = array('name');
// DEFINE RELATIONSHIPS --------------------------------------------------
// define a many to many relationship
// also call the linking table
public function users() {
return $this->belongsToMany('User', 'users_groups', 'group_id', 'user_id');
}
}
User Model
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
protected $fillable =array('email', 'username', 'password', 'password_temp', 'code', 'active', 'firstName','lastName','gender','dateOfBirth');
use UserTrait, RemindableTrait;
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'users';
/**
* The attributes excluded from the model's JSON form.
*
* #var array
*/
protected $hidden = array('password', 'remember_token');
// Each User BELONGS to many Groups
// Define our pivot table also
public function groups() {
return $this->belongsToMany('Group', 'users_groups', 'user_id', 'group_id');
}
}
All information about saving relations can be found here
You need attach()
$user = User::create(array(
'firstName' => $firstName,
// ...
));
$user->groups()->attach(1); // 1 being the group_id

Using different Table for Authentication in Laravel

I tried with the default user table for Laravel Auth purpose,
Now i am changing it to the admin Table
So i refereed here
Accordingly i tried to change the Table name as admin in the auth.php
'model' => 'User',
'table' => 'users',
to
'model' => 'AdminModel',
'table' => 'admin',
And in the AdminModel i have protected $table = 'admin';
And i got the error
Class AdminModel contains 6 abstract methods and must therefore be declared abstract or implement the remaining methods (Illuminate\Auth\UserInterface::getAuthIdentifier, Illuminate\Auth\UserInterface::getAuthPassword, Illuminate\Auth\UserInterface::getRememberToken, ...)
Is there anything is should change rather than here ?
Here is my AdminModel :
<?php
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
class AdminModel extends Eloquent implements UserInterface, RemindableInterface
{
protected $table = 'admin';
protected $fillable = array('UserName', 'Password');
public $timestamps = true;
public static $rules = array();
}
Update :
I replaced with appropriate changes
i.e.,
use UserTrait, RemindableTrait;
Solved,
It didn't ended in success
I checked
if (Auth::attempt(array('UserName' => $UserName, 'password' => $password)))
{
return Redirect::intended('dashboard');
}
else
{
$queries = DB::getQueryLog();
print_r(end($queries));
}
And it is always printing the query like this
Array ( [query] => select * from `admin` where `UserName` = ? limit 1 [bindings] => Array ( [0] => a ) [time] => 1 )
What might be the issue ?

laravel relationship one to one or one to many?

Table 1(users):
id username password role created_at updated_at
1 jhon yz 1 sometime sometime
Table 2(role):
id role created_at updated_at
1 employee sometime sometime
what I want
id username password role created_at updated_at
1 jhon yz employee sometime sometime
Users Model:
class User extends Eloquent implements UserInterface, RemindableInterface {
public function role()
{
return $this->hasOne('role', 'id', 'role');
}
protected $table = 'users';
protected $hidden = array('password', 'remember_token');
}
Roles Model:
class Role extends Eloquent {
protected $table = 'roles';
}
how do i get the role name in the view ? Is my relationship right
one role can be used by many users, like:
user1 can be admin
user2 can be admin too
user3 is a manager
user4 is a manager too
if thats the case then,..
Users Model:
class User extends Eloquent implements UserInterface, RemindableInterface {
protected $table = 'users';
protected $hidden = array('password', 'remember_token');
public function role()
{
return $this->belongsTo('Role');
}
}
Roles Model:
class Role extends Eloquent {
protected $table = 'roles';
public function users()
{
return $this->hasMany('User');
}
}
then use:
User::with('role')->whereRole(1)->get(); // assuming that 1 = employee; this is eager loading
this will yield something like:
[
new User([
'id' => 1,
'username' => 'blahblah',
'password' => 'pass',
'role' => 1,
'created_at' => '0000-00-00',
'updated_at' => '0000-00-00',
'role' => new Role([
'id' => 1,
'role' => 'employee',
'created_at' => '0000-00-00',
'updated_at' => '0000-00-00'
])
])
]

How to disable 'create_at' and 'update_at' in Laravel's seed file?

I don't want to use rows 'update_at' and 'create_at', but Laravel's seed file is trying to update it. How can I disable it?
Here is the code that I'm using:
use Illuminate\Database\Migrations\Migration;
class SeedUsersTable extends Seeder {
// $timestamps = false; <=== will return error
// public static $timestamps = false; <=== will return error
public function run()
{
DB::table('users')->delete();
User::create(array(
'id' => 1,
'name' => 'Админ',
'password' => Hash::make('admin'),
'login' => 'admin'
));
}
}
According to the Laravel docs,
... by default, Eloquent will maintain the created_at and updated_at columns on your database table automatically. Simply add these timestamp columns to your table and Eloquent will take care of the rest.
If you do not wish for Eloquent to maintain these columns, In your User model add the following:
class User extends Eloquent {
public $timestamps = false;
}
use Illuminate\Database\Migrations\Migration;
class SeedUsersTable extends Seeder {
public function run()
{
DB::table('users')->delete();
$user = new User(array(
'id' => 1,
'name' => 'Админ',
'password' => Hash::make('admin'),
'login' => 'admin'
));
$user->timestamps = false;
$user->save();
}
}

Categories