简体   繁体   English

Laravel 5.3注册后获取最后一个插入ID

[英]Laravel 5.3 get last insert id after registration

Using laravel5.3 php 5.6.3 使用laravel5.3 PHP 5.6.3

I want the last inserted id in users table for the redirected page after registration 我希望注册后在用户表中最后一个插入的id为重定向页面

So I want the last inserted id to userprofileadd.blade.php 所以我想最后插入的id到userprofileadd.blade.php

I have also tried ->with('id', $user->id) from register function 我也尝试过-> with('id',$ user-> id)从注册功能

I don't want automatic login after registration , so I removed the login part , and after registration the user will be redirected to another form , and i want the latest user id (who ever registered) from users table 我不想在注册后自动登录,所以我删除了登录部分,注册后,该用户将被重定向到另一种表单,并且我希望从users表中获得最新的用户ID(曾经注册过)

Register controller: 注册控制器:

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use App\role;
use App\Userdetails;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
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 = '/userprofileadd';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware(['auth', 'hr']);
    }

    /**
     * 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, [

            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|min:6|confirmed',
            'role'=>'required'
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        return User::create([

            'email' => $data['email'],
            'password' => bcrypt($data['password']),
            'role_id'=>$data['role'],
        ]);
    }

    public function showregistrationform()
    {
        $roles = role::all(); // get all teams
    return view('auth.register', [ 'roles' => $roles]);
    }
}

register function (i have commented out login after registration) 注册功能(注册后我已注释掉登录名)

 public function register(Request $request)
    {
        $this->validator($request->all())->validate();

        event(new Registered($user = $this->create($request->all())));

       // $this->guard()->login($user);

        return $this->registered($request, $user)
            ?: redirect($this->redirectPath());
            // ->with('id', $user->id)
    }

if you are using model then 如果您使用模型,那么

$user = new User();
$user->name = "JOHN";
$user->save();
$user->id; // contain the inserted id

if you are using db class 如果您正在使用db类

$id = DB::table('users')->insertGetId(
   ['email' => 'john@example.com', 'votes' => 0]
);

To get the last created user 获取最后创建的用户

$user = User::create([

    'email' => $data['email'],
    'password' => bcrypt($data['password']),
    'role_id'=>$data['role'],
]);

$this->lastCreatedUserId = $user->id;

To pass the userId to custom redirection page 将userId传递到自定义重定向页面

You may use the Laravel Auth redirectTo method . 您可以使用Laravel Auth redirectTo method Doc . Doc

protected function redirectTo()
{
    return route('customroutename', ['id' => $this->lastCreatedUserId]);
}

Example: 例:

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use App\role;
use App\Userdetails;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;

class RegisterController extends Controller
{
    public $lastCreatedUser;

    use RegistersUsers;

    protected $redirectTo = '/userprofileadd';

    //The redirectTo method will take precedence over the redirectTo attribute.
    protected function redirectTo()
    {
        //assuming your route name is 'userprofileadd' if not, use your route name of the route('/userprofileadd')
        return route('userprofileadd', ['id' => $this->lastCreatedUser]);
    }

    public function __construct()
    {
        $this->middleware(['auth', 'hr']);
    }

    protected function validator(array $data)
    {
        return Validator::make($data, [

            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|min:6|confirmed',
            'role'=>'required'
        ]);
    }

    protected function create(array $data)
    {
        $user = User::create([

            'email' => $data['email'],
            'password' => bcrypt($data['password']),
            'role_id'=>$data['role'],
        ]);

        $this->lastCreatedUser = $user->id;
        return $user;
    }

    public function showregistrationform()
    {
        $roles = role::all(); // get all teams
    return view('auth.register', [ 'roles' => $roles]);
    }
}

You can access the last created user in your UserprofileController's index method like, 您可以使用UserprofileController的index方法访问最后创建的用户,例如,

public function index($id)
{
    $lastCreatedUser = $id;
    //you may pass this variable to the view
}

Hope it helps.. Let me know the results.. 希望对您有所帮助。让我知道结果。

I'm not sure if this is what you're looking for or not, but you can do this to retrieve the latest record in the users table: 我不确定这是否是您要查找的内容,但是您可以这样做来检索users表中的最新记录:

$latestUser = App\User::latest()->first();

Hopefully this helps. 希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM