Laravel 5.4 邀请用户协作

Laravel 5.4 邀请用户协作

最终效果

file
发送邮件时在 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 如何传送到视图的。请大家解惑
file

本作品采用《CC 协议》,转载必须注明作者和本文链接
持续做一件事,必定有所成就
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 3
宇宙最厉害

赞一个,昨晚我也在看这个文章。:+1:

7年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!