I've created a new table called "admins" table. I want to use this instead of the users table.
I've already added this in my User model:
protected $table = 'admins';
And I also tried this on my auth.php:
'users' => [
'driver' => 'database',
'table' => 'admins',
],
But none of them worked. Are there any other solutions that could work?
For one, you must also change the RegisterController, in it you will find the create method:
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
As you can see it too uses the User model.
Related
i am trying to upload an image as a profile picture in laravel bootstrap auth package.
in this i am trying to change some package files to upload image. also i added a column in users table.
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'campus_id' => $data['campus_id'],
'role' => $data['role'],
'remarks' => $data['remarks'],
'image' => $data['image'],
]);
}
i make changes in Auth controller in validation function
also makes some changes in user store function
I think you need to move user profile image before create its entry inside database.
protected function create(array $data)
{
$imageName = time().'.'.$data['image']->extension();
//$data['image']->move(public_path('images'), $imageName);
$data['image']->storeAs('public/images', $imageName);
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'campus_id' => $data['campus_id'],
'role' => $data['role'],
'remarks' => $data['remarks'],
'image' => $imageName,
]);
}
You can use Image intervention for this. After installing you can use it in your controller as use Image;
$image = $request->file('image');
$img_name = hexdec(uniqid()).'.'.$image->getClientOriginalExtension();
Image::make($image)->resize( 847.5, 431 )->save('uploads/sliders/'.$img_name);
$image_path = 'uploads/sliders/'.$img_name;
Slider::create([
'title' => $request->title,
'image' => $image_path,
'created_at' => Carbon::now()
]);
1st you need to move your image to your desired directory inside public folder and save that directory in the database.
Need a little help with Laravel 8, I have two users with the same attributes but the other one has another attribute :
Seeker: name, email, password
Agent: name, email, password, broker license no
This is my form:
If the user registers as an agent, other fields require only for an agent.
I'm having trouble on how to store it in a database.
protected function create(array $data)
{
$r_id = isset($data['agent']) ? 2 : 3;
return User::create([
'given_name' => $data['given_name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'role_id' => $r_id, ]);
}
you can create another table for information agents and use relationship with column id in table users
Sample code :
$user = User::create([
'given_name' => $data['given_name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'role_id' => $r_id
]);
if($r_id === 2){
Agent::create([
'user_id' => $user->id,
.
.
...
]);
}
User relation to Agent :
public function agent()
{
return $this->belongsTo(Agent::class, 'id', 'user_id');
}
In my application, I have tables related to users and some of these tables require a row be inserted as soon as a User is registered.
For example, I have the following models:
UserProfile,
UserAssignedRole,
UserSettings
As soon as a User successfully registers, they will need a corresponding row in the DB.
I've done the following:
protected function create(array $data)
{
$user = User::create([
'username' => $data['username'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
UserProfile::create([
'user_id' => $user->id,
]);
UserAssignedRole::create([
'user_id' => $user->id,
'role_id' => 1
]);
UserSettings::create([
'user_id' => $user->id,
'nsfw' => 1
]);
return $user;
}
I'm looking for a better and more elegant way to achieve this. Is there a "Laravel" way in achieving this?
In the Larvel docs there's a chapter about Recursively Saving Models & Relationships. This is probably what you're looking for. They specify the following example:
$post = App\Post::find(1);
$post->comments[0]->message = 'Message';
$post->comments[0]->author->name = 'Author Name';
$post->push();
You could achieve something similar by creating a user including all its relationships in one push().
I am using Laravel Framework, I have generated all Register and Login through "$php artisan make:auth" command, now I have added a new column called "avatar" in the users table, and I want to set it to "noimage.jpg", so each time I register by default "noimage.jpg" will be added.
RegisterController
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'username' => $data['username'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'avatar' => 'noimage.jpg' //How it suppose to be?
]);
}
You also have to add avatar to the $fillable property of your model. Otherwise you cannot assign it with create. See docs on Mass Assignment.
Instead you could manually assign the avatar:
protected function create(array $data)
{
$user = User::create([
'name' => $data['name'],
'username' => $data['username'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
$user->avatar = 'noimage.jpg';
return $user;
}
Another way to set a default value for your model is to use Laravel lifecycle:
const DEFAULT_AVATAR = 'noimage.jpg'
protected static function boot()
{
parent::boot();
static::creating(function (User $user) {
if (!$user->avatar) {
$user->avatar = self::DEFAULT_AVATAR;
}
});
}
See: https://laravel.com/docs/5.8/eloquent#events
I'm using Laravel 5.3 and used the make:auth artisan command to scaffold the login/registration system. I'm doing my login as companies, so I have a table called Company. How do I change the original sql to go get the email and password from the Company table instead of the User table?
I already tried to change in the config/auth.php file in the providers part, but when I changed 'model' => App\User::class, to 'model' => App\Company::class,, it started logging in, but regardless if the email and password input were completely wrong. Any ideas?
EDIT: After the Company registers and logs in, it has the ability to invite Users, therefore the original User table has to remain
Laravel 5.3 has changes in the Auth implementation. For me, this way solved it:
First, provide a company table in the database that fulfils the criteria to be used for identification. Thus, it needs a name, email, password and remember_token column. Details can be found here.
In the config/auth.php change the users model to your company class.
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Company::class,
],
Create a Company class in the App folder that extends the Auth, so use:
use Illuminate\Foundation\Auth\User as Authenticatable;
In the Company class, define fillable and hidden fields.
class Company extends Authenticatable {
protected $fillable = [
'name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
}
In the RegisterController.php change "use App\User" to
use App\Company;
Adjust the create and validator function in the RegisterController.php with Company::create
protected function create(array $data)
{
return Company::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:companies',
'password' => 'required|min:6|confirmed',
]);
}
'email' => 'required|email|max:255|unique:companies'
(table name for Company Model will be companies)
Hope this helps!
DO NOT FORGET TO CHANGE VALIDATION IN REGISTERCONTOLLER.PHP AS WELL.
from
'email' => 'required|email|max:255|unique:users',
to
'email' => 'required|email|max:255|unique:company',