Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
126 views
in Technique[技术] by (71.8m points)

How to create an invitation system in Laravel PHP

Im new in laravel and I'm watching a youtube playlist to build my 1st project.... now I'm trying to implement an inv only system so the register form looks like

username:
inv code:
password:

I've already created a migration which has

user_id - this is user id of the user who created the invite
code - the code which will get validated at time of registration and then deleted
{timestamps}

so the issue im having is: how do I verify that the invite entered by user exists in the table "inv_codes" and matches the column "code" and then delete it after registration is complete

here is a minimal reproducible example

lass RegisterController extends Controller
{
    public function __construct()
    {
        $this->middleware(['guest']);
    }

    public function index()
    {
        return view('auth.register');
    }

    public function submit(Request $request)
    {
        $this->validate($request, [
            'username' => 'required|max:6',
            'password' => 'required',
        ]);

        User::create([
            'username' => $request->username,
            'password' => Hash::make($request->password),
        ]);

        auth()->attempt($request->only('username', 'password'));
        return redirect()->route('dashboard');
    }
}
question from:https://stackoverflow.com/questions/65875015/how-to-create-an-invitation-system-in-laravel-php

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Suppose that you have code column in both users and inv_codes tables. Now during registration, you can use validation. Something like this:

$request->validate([
        'code' => 'required|exists:inv_codes,code',
        // ... rest of registration fields
    ]);

Then you can delete that row in inv_codes table by something like this: You can read more at: https://laravel.com/docs/8.x/validation#quick-writing-the-validation-logic

DB::table('inv_codes')->where('code', $request->code)->delete();

You can read more at: https://laravel.com/docs/8.x/queries#delete-statements


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...