Im using the default registration and log in form of laravel. What i want to happen is how do i store a data after the user successfully register. I tried doing this but its not storing any data in the Time_tracker table and there is no any error occured. Can some one help me?
AuthController.php
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use Illuminate\Mail\Mailer;
use Illuminate\Http\Request;
use App\Time_tracker;
class AuthController extends Controller
{
/*
|--------------------------------------------------------------------------
| Registration & Login Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users, as well as the
| authentication of existing users. By default, this controller uses
| a simple trait to add these behaviors. Why don't you explore it?
|
*/
use AuthenticatesAndRegistersUsers, ThrottlesLogins;
/**
* Where to redirect users after login / registration.
*
* #var string
*/
protected $redirectTo = 'maintenance';
/**
* Create a new authentication controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware($this->guestMiddleware(), ['except' => 'logout']);
}
/**
* Get a validator for an incoming registration request.
*
* #param array $data
* #return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'company' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'telephone' => 'required|max:255',
'password' => 'required|min:6|confirmed',
'g-recaptcha-response' => 'required|captcha',
]);
}
/**
* Create a new user instance after a valid registration.
*
* #param array $data
* #return User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'company' => $data['company'],
'email' => $data['email'],
'telephone' => $data['telephone'],
'password' => bcrypt($data['password']),
]);
$user = new Time_tracker;
$user->current_time = 0;
$user->purchase_time = 0;
$user->status = 'not_paid';
$user->user_id = $id;
$user->save();
}
}
You are returning the created user that's why the code below it isn't running, you should remove return and place it at the end in this way:
protected function create(array $data)
{
$user = User::create([ // <------ Removed 'return' from the front of this line
'name' => $data['name'],
'company' => $data['company'],
'email' => $data['email'],
'telephone' => $data['telephone'],
'password' => bcrypt($data['password']),
]);
$time_tracker = new Time_tracker();
$time_tracker->current_time = 0;
$time_tracker->purchase_time = 0;
$time_tracker->status = 'not_paid';
$time_tracker->user_id = $user->id; // <----- This would be '$user->id' instead of '$id'
$time_tracker->save();
return $user; // <----- 'return' added at the end of the method
}
Hope this helps!
Related
How can I stop users from registering an existing name in the database? I would like to stop this from happening and I am using Laravel 5.5
for example: there is a user callled John Doe and then a new user registers the same name and i would want to stop this from happening.
This is my register controller.php
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
use RegistersUsers;
/
* Where to redirect users after registration.
*
* #var string
*/
protected $redirectTo = '/home';
/
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('guest');
}
/
* Get a validator for an incoming registration request.
*
* #param array $data
* #return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|string|min:3|max:255|alpha|',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
]);
}
/
* Create a new user instance after a valid registration.
*
* #param array $data
* #return \App\User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
}
Please help
You just put an unique validation to the users' name column:
return Validator::make($data, [
'name' => 'required|string|min:3|max:255|alpha|unique:users',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
]);
PS: The question is, why would you do that? There are a lot of persons having the same name.
I would like to send some emails after users registered.
But, it doest work. I didn't receive an email in my SMTP mail. I'm using mailtrap.io.
I have set up a Registered event with a listener to NewUserRegistered.
Within my NewUserRegistered Controller\Auth\AuthController as follows:
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Log;
use Validator;
use Mail;
class AuthController extends Controller {
/*
|--------------------------------------------------------------------------
| Registration & Login Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users, as well as the
| authentication of existing users. By default, this controller uses
| a simple trait to add these behaviors. Why don't you explore it?
|
*/
use AuthenticatesAndRegistersUsers, ThrottlesLogins;
/**
* Where to redirect users after login / registration.
*
* #var string
*/
protected $redirectTo = '/';
/**
* Create a new authentication controller instance.
*
* #return void
*/
public function __construct() {
$this->middleware($this->guestMiddleware(), [
'except' => 'logout'
]);
}
/**
* Get a validator for an incoming registration request.
*
* #param array $data
* #return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data) {
return Validator::make($data, [
'name' => 'required|max:255',
'matric' => 'required|max:5',
'faculty' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
'address' => 'required',
'phone' => 'required',
]);
}
/**
* Create a new user instance after a valid registration.
*
* #param array $data
* #return User
*/
protected function create(array $data) {
return User::create([
'name' => $data['name'],
'matric' => $data['matric'],
'faculty' => $data['faculty'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'address' => $data['address'],
'phone' => $data['phone'],
]);
event(new Registered($user));
//sreturn User;
}
}
Within my Registered Events as follows:
<?php
namespace App\Events;
use App\Events\Registered;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class Registered extends Event
{
use SerializesModels;
/**
* #var
*/
public $user;
/**
* Create a new event instance.
*
* #param $user
*/
public function __construct($user)
{
//
$this->user = $user;
}
/**
* Get the channels the event should be broadcast on.
*
* #return array
*/
public function broadcastOn()
{
return [];
}
}
Within my NewUserRegistered Listeners as follows:
<?php
namespace App\Listeners;
use App\Events\Registered;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Mailer;
use Mail;
class NewUserRegistered
{
/**
* Create the event listener.
*
* #return void
*/
public function __construct(Mailer $mailer)
{
$this->mailer = $mailer;
}
/**
* Handle the event.
*
* #param Registered $event
* #return void
*/
public function welcome(Registered $event)
{
$data = [
'user' => $event->user,
'from' => 'hello#test.dev',
'subject' => 'Welcome to test'
];
$this->mailer->send('emails.auth.verify', $data, function($message) {
$message->to($data['user']->email, $data['user']->matric)
->subject($data['subject']);
});
}
}
Your event never fires because you're returning the user after creating them. Your code should have thrown errors when you attempted. Change your code to this.
use App\Events\Registered;
protected function create(array $data)
{
$user = User::create([
'name' => $data['name'],
'matric' => $data['matric'],
'faculty' => $data['faculty'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'address' => $data['address'],
'phone' => $data['phone'],
]);
event(new Registered($user));
return $user;
}
Also be sure to register your event listener.
A couple potential issues with your solution could be:
In Controller\Auth\AuthController, you haven't imported the Registered event into the namespace. Try adding use App\Events\Registered; to the top.
In NewUserRegistered, you've defined a #welcome method, but Laravel event listeners expect a #handle method. See the Laravel docs on Events for more information.
Beyond that, you could be experiencing issues with your Mailtrap credentials.
I seen this question before I just wanted help clarifying it.
The goal is to Fire an event when someone registers to the site.
I added the Event/Listener Pair in the Event Service Provider as noted in the Laravel Documentation.
Then I ran the: php artisan command
php artisan generate:event
When a user signs up at example.com/register it should trigger that event. However it does not.
Any Suggestions?
app/Http/Controllers/Auth/AuthController.php
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
Event::fire(new NewUserSignUp($data));
}
The whole file:
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use App\Events\NotifyAdminSignUp;
class AuthController extends Controller
{
/*
|--------------------------------------------------------------------------
| Registration & Login Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users, as well as the
| authentication of existing users. By default, this controller uses
| a simple trait to add these behaviors. Why don't you explore it?
|
*/
use AuthenticatesAndRegistersUsers, ThrottlesLogins;
/**
* Where to redirect users after login / registration.
*
* #var string
*/
protected $redirectTo = '/';
/**
* Create a new authentication controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware($this->guestMiddleware(), ['except' => 'logout']);
}
/**
* Get a validator for an incoming registration request.
*
* #param array $data
* #return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
]);
}
/**
* Create a new user instance after a valid registration.
*
* #param array $data
* #return User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
Event::fire(new NewUserSignUp($data));
}
}
protected function create(array $data)
{
//fire event BEFORE return
Event::fire(new NewUserSignUp($data));
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
//because nothing after this line will ever be processed
}
I searched through the internet regarding to this problem and I followed the instructions(every website that I saw) but im having a problem to this error "Class 'App\Http\Controllers\Auth\Registered' not found". Is there any lack of codes in my controller? how do i fix this kind of error?
My controller
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use App\Http\Controllers\Auth;
use Illuminate\Http\Request;
use App\Http\Requests;
class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
use RegistersUsers;
/**
* Where to redirect users after registration.
*
* #var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* Get a validator for an incoming registration request.
*
* #param array $data
* #return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'first_name' => 'required|max:255',
'middle_name' => 'required|max:255',
'last_name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
]);
}
/**
* Create a new user instance after a valid registration.
*
* #param array $data
* #return User
*/
protected function create(array $data)
{
return User::create([
'first_name' => $data['first_name'],
'middle_name' => $data['middle_name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
public function register(Request $request)
{
$this->validator($request->all())->validate();
event(new Registered($user = $this->create($request->all())));
Mail::to($ser->email)->send(new ConfirmationEmail($user));
return back()->with('status','Please confirm your email address');
}
}
use Illuminate\Auth\Events\Registered;
SOLUTION (import the namespace at the top of the Registercontroller, like below)
use Illuminate\Auth\Events\Registered;
I'm using the default Laravel 5.1 user registration. I have two tables: users and shops. When user registers, the app should insert a user in the table users, get the id and use it to register a shop. I've been reading the default AuthController.php but i didn't find anything. Here is the AuthController if it helps.
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
class AuthController extends Controller
{
/*
|--------------------------------------------------------------------------
| Registration & Login Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users, as well as the
| authentication of existing users. By default, this controller uses
| a simple trait to add these behaviors. Why don't you explore it?
|
*/
use AuthenticatesAndRegistersUsers, ThrottlesLogins;
/**
* Create a new authentication controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('guest', ['except' => 'getLogout']);
}
/**
* Get a validator for an incoming registration request.
*
* #param array $data
* #return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
//'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',
]);
}
/**
* Create a new user instance after a valid registration.
*
* #param array $data
* #return User
*/
protected function create(array $data)
{
return User::create([
//'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
/**
* Get the path to the login route.
*
* #return string
*/
public function loginPath()
{
return route('login');
}
/**
* Get the post register / login redirect path.
*
* #return string
*/
public function redirectPath()
{
return route('home');
}
}
Solved, but now I have a Integrity constraint violation. Is this code correct?
protected function create(array $data)
{
$user = new User([
'email' => $data['email'],
'password' => bcrypt($data['password'])
]);
$user->role = 'shop_owner';
$user->remember_token = str_random(10);
$user->save();
$userId = $user->id;
Shop::create([
'name' => $data['s_name'],
'address' => $data['s_address'],
'CP' => $data['s_pcode'],
'Telephone' => $data['s_tlf'],
'contact_name' => $data['cp_name'],
'contact_num' => $data['cp_tlf'],
'id_user' => $userId
]);
return $user;
}
There you go:
protected function create(array $data)
{
$user = User::create([
//'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
$userId = $user->id;
Shop::create([... use $userId here ...]);
return $user;
}
This goes to your controller:
public function store(Request $request) {
$user = User::create(Input::all());
$user->save();
$shop = Shop::create([..enter shop attributes or leave blank..]);
$user->shop()->save($shop);
}
You need to place the following code at the top of the Auth Controller
use App\Shop;