Skip to content

Instantly share code, notes, and snippets.

@eugenfranktz
Last active February 1, 2017 11:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eugenfranktz/7f4a882d5ee4b31a2e9898e1a7e1a7a2 to your computer and use it in GitHub Desktop.
Save eugenfranktz/7f4a882d5ee4b31a2e9898e1a7e1a7a2 to your computer and use it in GitHub Desktop.
mass assignment error on email_token ...
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password', 'email_token', 'verified'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function verified()
{
$this->verified = 1;
$this->email_token = null;
$this->save();
}
}
<?php
namespace Illuminate\Foundation\Auth;
use App\Mail\EmailVerification;
use DB;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Auth\Events\Registered;
use Mail;
trait RegistersUsers
{
use RedirectsUsers;
/**
* Show the application registration form.
*
* @return \Illuminate\Http\Response
*/
public function showRegistrationForm()
{
return view('auth.register');
}
/**
* Handle a registration request for the application.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function register(Request $request)
{
// Laravel validation
$validator = $this->validator($request->all());
if ($validator->fails())
{
$this->throwValidationException($request, $validator);
}
// Using database transactions is useful here because stuff happening is actually a transaction
// I don't know what I said in the last line! Weird!
DB::beginTransaction();
try
{
$user = $this->create($request->all());
// After creating the user send an email with the random token generated in the create method above
$email = new EmailVerification(new User(['email_token' => $user->email_token]));
Mail::to($user->email)->send($email);
DB::commit();
return back();
}
catch(Exception $e)
{
DB::rollback();
return back();
}
}
/**
* Get the guard to be used during registration.
*
* @return \Illuminate\Contracts\Auth\StatefulGuard
*/
protected function guard()
{
return Auth::guard();
}
/**
* The user has been registered.
*
* @param \Illuminate\Http\Request $request
* @param mixed $user
* @return mixed
*/
protected function registered(Request $request, $user)
{
//
}
}
//controller
<?php
namespace App\Http\Controllers\Auth;
use DB;
use Mail;
use App\User;
use Validator;
use Illuminate\Http\Request;
use App\Mail\EmailVerification;
use App\Http\Controllers\Controller;
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|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']),
'email_token' => str_random(10),
]);
}
public function verify($token)
{
// The verified method has been added to the user model and chained here
// for better readability
User::where('email_token',$token)->firstOrFail()->verified();
return redirect('login');
}
}
@introwit
Copy link

introwit commented Feb 1, 2017

  1. Change the gist extension from .txt to .php & remove the comment on the first line. Also remove the opening php tags in between to preserve the formatting.

  2. You shouldn't make any changes in the TRAIT , as said in the blog override the trait's method in the controller i.e. the register() method should be in the registercontroller and not in the trait.

  3. Everything else looks perfect. Since you have already forked my repo you can start working on it and upload the repo you have been working so I can test it myself what's going wrong.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment