@schwarzgepard
Schema::create('invites', function (Blueprint $table) {
$table->increments('id');
$table->string('token')->index();
$table->string('name');
$table->string('email');
$table->boolean('accepted')->default(0);
$table->timestamp('accepted_at')->nullable();
$table->timestamps();
});
The Model (abbreviated)
Notice the get route key name method for route model binding on the create controller method. It accepts the token and returns the applicable row
<?php
namespace App\Site\Models;
use Illuminate\Database\Eloquent\Model;
class Invite extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'token',
'name',
'email',
'accepted',
'accepted_at',
];
/**
* Get the route key for the model.
*
* @return string
*/
public function getRouteKeyName()
{
return 'token';
}
/**
* Many to many relationship to Role model.
*
* @return object
*/
public function roles()
{
return $this->belongsToMany(Roles::class)
}
/**
* Add a new invite and attach role(s) if applicable.
*
* @param object $request
* @return object $invite
*/
public function addNew($request)
{
$invite = $this->create($request->all());
if($request->has('roles')) {
$invite->roles()->attach($request->roles);
return $invite->load('roles');
}
return $invite;
}
/**
* Process an invited request to sign up/register
* Find the invite by email, update accepted st and accepted boolean
* Create a new User on the user model
* Add applicable roles and return user object
*
* @param object $request
* @return User $user
*/
public function processInvited($request)
{
$invited = $this->with('roles')->where('email', $request->email)->first();
if($invited) {
$invited->update(['accpeted' => 1, 'accpeted_at' => \Carbon\Carbon::now()->toDateTimeString()]);
$user = User::create([
'name' => $invite->name,
'email' => $invite->email,
'password' => $request->password,
'active' => 1,
]);
if($invited->roles) {
$user->roles()->attach($invited->roles());
}
return $user;
}
return abort('404', 'No invite was found');
}
}
Model Observer (abbreviated)
<?php
namespace App\Site\Observers
use App\Site\Models\Invite;
class Invites
{
/**
* Listen to the Invite creating event.
*
* @param Invite $invite
* @return void
*/
public function creating(Invite $invite)
{
$invite->token = $this->generateToken();
}
/**
* Listen to the Invite created event.
*
* @param Invite $invite
* @return void
*/
public function created(Invite $invite)
{
event(new NewInviteWasCreated($invite));
}
/**
* Generate random token, check if unique, if not regenerate.
*
* @return string $token
*/
protected function generateToken()
{
$token = str_random(10);
if(Invite::where('token', $token)->first()) {
return $this->generateToken();
}
return $token;
}
}
Notice the created method fires an event to send the email.
Front end controller, You don't NEED to use two controllers but in the app where I copied the code it had a separate backend controllers.
class InviteController extends Controller
{
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('front.site.invite.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(InvitedForm $request, Invite $invite)
{
$user = $invite->addNew($request);
return redirect()->route('account.show', $user);
}
The newer mail in 5.3 is pretty stright forward (The above code is from a 5.2, so the mail is different).
https://laravel.com/docs/5.3/mail#generating-mailables
Basically send your data and it formats the email for you.
Check this out!!!!
https://laracasts.com/series/whats-new-in-laravel-5-3/episodes/6