Ok so we can create a new user:
$user = new User;
$user->name = 'Megauser'
$user->save();
And then we can add some user items:
$item = new Item;
$item->user_id = $user->id; //id we got from a freshly created user
$item->quantity = '9000';
$item->save();
But what if we mass assign the user?
User::create([
'name' => 'Megauser'
)];
The create method will return the new object with its ID.
$user = User::create([
'name' => 'Megauser'
)];
$userId = $user->id
Or as Rob Gordijn pointed out below, if you are worried about key names, you can use the getKey() call.
$userId = $user->getKey();
This is necessary if someone overrides the column name in their Eloquent model. For example:
class Example extends Eloquent {
$primaryKey = 'uid';
}
$user = User::create([
'name' => 'Megauser'
)];
I would go for
<?php
$User = User::create([
'name' => 'Huzzah'
)];
$UserKey = $User->getKey();
to ensure your still works when someone decides to changes the column name ;)
Related
I was trying to send a password reset link to my newly created user and had a hard time trying to find a solution. In case someone has the same issue here is what worked for me in Laravel 7.
Or is there a better way to handle all: add entry in password_resets table and send the notification?
Mail sending properly but i want to send password reset mail to user .Below is the my add controller
public function contactSave(Request $request)
{
$password = Hash::make($request->name);
$validate = $request->validate([
'name' => 'required|regex:/^[\pL\s\-]+$/u',
'email' => 'email',
'phone' => 'digits:10',
'address' => 'required',
'country' => 'required',
'state' => 'required',
// 'comment' => 'required',
'organization' => 'required',
'captcha' => 'required|captcha'
],
[
'captcha.captcha' => 'Incorrect Captcha'
]
);
$user= new User;
$user->name = $request->name;
$user->email = $request->email;
$user->password = $password;
$user->save();
$CustomerContact= new CustomerContact;
// $CustomerContact->name = $request->name;
// $CustomerContact->email = $request->email;
$CustomerContact->phone = $request->phone;
$CustomerContact->address = $request->address;
$CustomerContact->user_id = $user->id;
$CustomerContact->country_id = $request->country;
$CustomerContact->state_id = $request->state;
$CustomerContact->comment = $request->comment;
$CustomerContact->organization = $request->organization;
$CustomerContact->captcha = $request->captcha;
$CustomerContact->save();
$details = [
'title' => 'Hi '.$request->name.'',
'body' => 'Your account has been created successfully. Request you set your password with this link ???'
];
\Mail::to($request->email)->send(new \App\Mail\ContactMail($details));
return redirect('contacts')->with('success', 'User created successfully.');
}
Anyone here to help me. After saving data in user and employee . I want send email with password reset link to that user id email($request->email).
GIve me simplest solution with kittle explation how to do this
if i correctly understood your problem you can create a route for example: password/reset/user-code
and send it in your email to every user.
you should create a random-code for every user when its created and put it in that link and send in your email.
when user click on that link it will come to your controller and you check that random-code in your database and find your user! and when you have your user you can show them your view for resetting password and save new password
that what i did for password reset before
IF you want to send some data (link or token or everything else) to your mail use this:
Mail::to($request->email)->send(new ContactMail($your_token,$link,$user));
and in your ContactMail class:
class ContactMail extends Mailable
{
use Queueable, SerializesModels;
public $your_token;
public $link;
public $user;
public function __construct($your_token,$link,$user)
{
$this->your_token= $your_token;
$this->link= $link;
$this->user= $user;
}
public function build()
{
return $this->view('email')
->with('your_token',$this->your_token)
->with('link',$this->link)
->with('user',$this->user
}
}
I created a UserController where admin can create a user with special privileges, asides from the Laravel Automatic register page, the issue is user creation works fine via the UserController but I can't sign in with the details I used when creating the account. Would appreciate any help I can find on here.
public function create()
{
//
$privileges = privilege::all();
$courses = course::all();
return view('tutor.create')->with('privileges', $privileges)->with('courses',$courses);
}
public function store(Request $request)
{
//
$this->validate($request,[
'name' => 'required',
'email' => 'required',
'password' => 'required',
]);
$users = new User;
$users->name = $request->input('name');
$users->email = $request->input('email');
$users->password = \Hash::make($request['password']);
$users->privilege_id = $request->input('privilege_id');
$users->save();
$course = $request->get('course');
$users->courses()->attach($course);
return redirect('/tutor')->with('success','User created');
}
Use model is:
public function details()
{
return $this->hasOne(UserDetails::class, 'user_id', 'id');
}
First I create user model:
$user = User::create([
'email' => $request->inn,
'password' => Hash::make($request->password),
]);
Then I tried to assign details of users:
$user->details()->create($user_details);
It returns me this error:
Call to a member function details() on null
You have not provided a lot of information with this issue however I would suggest that the issue is that the User is not being created because your attributes on the User model are guarded.
Make sure you have one of the following solutions on the your User model
protected $fillable = [
'email',
'password'
]
Or
protected $guarded = []
Failing to add these means that Laravel will not allow you to add details to the model using anything other than save() method
Two solutions for this.
Create method
If you are using create method it's not creating object as you're doing right now.
$userid = User::create([
'email' => $request->inn,
'password' => Hash::make($request->password),
])->id;
User::find($userid)->details()->create($user_details);
In your question $user is not getting object that's why this error you are getting.
Object Method
$user = new User;
$user->email = $request->inn;
$user->password= Hash::make($request->password);
$user->save();
//here $user is an instance of User so if you dd($user) you'll get an object.
$user->details()->create($user_details);
Hope this will work for you.
I'd like to improve this piece of code when updating a related model (1 to 1) on laravel and also know if this is right, wrong, right but not recommended, etc. Thanks all.
$user = User::find($id);
$user->name = $request->input('name');
$user->email = $request->input('email');
$user->update();
$tenant = Tenant::where('user_id', $id)->first();
$tenant->suite_number = $request->input('suite_number');
$tenant->update();
First you have to define relationship of user and tenant in you user model:
// User.php
public function tenant()
{
return $this->hasOne('App\Tenant');
}
As this is an one to one model, you can update your related model like following. Make sure the relation model also exists.
$user = User::find($id);
$user->name = $request->input('name');
$user->email = $request->input('email');
$user->tenant->suite_number = $request->input('suite_number');
$user->push();
i believe it could be
// you can omit the input() if u want to
$user = User::find($id);
$user->update([
'name' => $request->name;
'email' => $request->email;
]);
$tenant = Tenant::where('user_id', $id)->first();
$tenant->update([
'suite_number' => $request->suite_number;
]);
for eager loading it would be
$user = User::with('tenant')->find($id);
$user->update([
'name' => $request->name;
'email' => $request->email;
'tenant.suite_number' => $request->suite_number;
]);
or reverse relation
$tenant = Tenant::with('user')->where('user_id', $id)->first();
$tenant->update([
'suite_number' => $request->suite_number;
'user.name' => $request->name;
'user.email' => $request->email;
]);
I am working with Laravel 4 validation right now. My basic setup is done and tested. I can fill the form in view and submit to controller. I can save all details to database using model. I am having a problem now with validation.
I use Input::get() to capture each of the posted variables in the controller. I read that validation should ideally be done in the model. where am I supposed to invoke the validator? Model or Controller? and how am I supposed to pass the validator the $input? is it an array of all variables posted or am I missing something?
Laravel 4 documentation really fails to illustrate with examples answers to common usage questions.
This is the validator I set up in my model:
public static function validate($input)
{
$rules = array(
# place-holder for validation rules
'firstname' => 'Required|Min:3|Max:40|Alpha',
'lastname' => 'Required|Min:3|Max:40|Alpha',
'email' => 'Required|Between:3,64|Email|Unique:users',
'country' => 'Required',
'password' =>'Required|AlphaNum|Between:7,15|Confirmed',
'password_confirmation'=>'Required|AlphaNum|Between:7,15'
);
# validation code
$validator = Validator::make($input, $rules);
/*if( $validator->passes() ) {
} else {
# code for validation failure
}*/
}
controller:
public function register()
{
/*Create new user if no user with entered email exists. Use validator to ensure all fields are completed*/
$user = new User;
/*Handle input in POST*/
$email = Input::get('email');
$password = Input::get('password');
$passwordConfirmed = Input::get('password_confirmation');
$firstName = Input::get('firstname');
$lastName = Input::get('lastname');
$country = Input::get('country');
$user->email = $email;
$user->password = Hash::make($password);
$user->firstname = $firstName;
$user->lastname = $lastName;
$user->country = $country;
//$user->save();
$this->layout->content = View::make('test');
}
and I've been following this link so far when it comes to validation. Please help as I am new to this framework
You are missing the input in the validator, you did not defined it
Use it like
$input = Input::all();
$validator = Validator::make($input, $rules);
or
$validator = Validator::make(Input::all(), $rules);
and take a look at the forums, this will help you more than that blog: http://forums.laravel.io/viewtopic.php?id=12104