Laravel 5.4 邀请用户协作
Laravel 5.4 邀请用户协作
最终效果
发送邮件时在 invites
表添加一条数据,在邮件验证成功后,添加到user
表中。代码没有判断重复邀请
可以给一个邮件发多个邀请,在已经添加到user表后,再次邮件中点击会报重复错误。
1. 前提,配置好邮件发送
请参考 Laravel 5.4 邮箱验证
2. 配置数据库相关
2.1 创建数据库
create database Invit default charset utf8
2.2 配置链接数据库.env
文件
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=Invit
DB_USERNAME=root
DB_PASSWORD=
2.3 修改 User
的数据库迁移,去掉了名称和密码
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('email')->unique();
$table->rememberToken();
$table->timestamps();
});
}
2.4 创建 invites
表迁移
php artisan make:migration create_invites_table //创建数据库迁移文件
public function up()
{
Schema::create('invites', function (Blueprint $table) {
$table->increments('id');
$table->string('email');
$table->string('token', 16)->unique();
$table->timestamps();
});
}
public function down()
{
Schema::drop('invites');
}
2.5 执行数据库迁移
php artisan migrate
3. 模型
3.1 Invite
模型
php artisan make:model Invite //创建Invite模型
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Invite extends Model
{
protected $fillable = [
'email', 'token',
];
}
4.路由
Route::get('invite', 'InviteController@invite')->name('invite');
Route::post('invite', 'InviteController@process')->name('process');
// {token} is a required parameter that will be exposed to us in the controller method
Route::get('accept/{token}', 'InviteController@accept')->name('accept');
5.控制器
php artisan make:controller InviteController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
use App\Invite;
use App\Mail\InviteCreated;
use Illuminate\Support\Facades\Mail;
class InviteController extends Controller
{
public function invite()
{
// show the user a form with an email field to invite a new user
return view('invite');
}
public function process(Request $request)
{
// validate the incoming request data
do {
//generate a random string using Laravel's str_random helper
$token = str_random();
} //check if the token already exists and if it does, try again
while (Invite::where('token', $token)->first());
//create a new invite record
$invite = Invite::create([
'email' => $request->get('email'),
'token' => $token
]);
//dd($invite);
// send the email
Mail::to($request->get('email'))->send(new InviteCreated($invite));
// redirect back where we came from
return redirect()
->back();
}
public function accept($token)
{
// Look up the invite
if (!$invite = Invite::where('token', $token)->first()) {
//if the invite doesn't exist do something more graceful than this
abort(404);
}
// create the user with the details from the invite
User::create(['email' => $invite->email]);
// delete the invite so it can't be used again
$invite->delete();
// here you would probably log the user in and show them the dashboard, but we'll just prove it worked
return 'Good job! Invite accepted!';
}
}
6.邮件类
php artisan make:mail InviteCreated
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Invite;
class InviteCreated extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct(Invite $invite)
{
$this->invite = $invite;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
$invite=$this->invite;
//return $this->view('view.name');
return $this->from('kzh4435@163.com')
->view('emails.invite',compact('invite'));
}
}
大功告成
参考
https://laravel-news.com/user-invitation-system
疑问
在原文邮件类的build()
方法中
public function build()
{
$invite=$this->invite;
//return $this->view('view.name');
return $this->from('kzh4435@163.com')
->view('emails.invite',compact('invite'));
}
原文并没有传送参数invite
,没有明白$invite
如何传送到视图的。请大家解惑
本作品采用《CC 协议》,转载必须注明作者和本文链接
赞一个,昨晚我也在看这个文章。:+1:
https://github.com/illuminate/mail/blob/ma...
有心了,gif好帅