dqu92800 2018-05-10 11:37
浏览 35

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

Using laravel5.3 php 5.6.3

I want the last inserted id in users table for the redirected page after registration

So I want the last inserted id to userprofileadd.blade.php

I have also tried ->with('id', $user->id) from register function

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

Register controller:

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use Appole;
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)
    }
  • 写回答

3条回答 默认 最新

  • dongnanke4106 2018-05-10 11:58
    关注

    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:

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

    Hopefully this helps.

    评论

报告相同问题?

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本