在Laravel 5.3中注册新类型的用户 [英] Register new type of user in Laravel 5.3

查看:70
本文介绍了在Laravel 5.3中注册新类型的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在laravel中创建两种类型的用户,为此我有两个表.我想使用laravel中定义的注册视图和控制器,但是当我注册新用户时,它仅保存在第一个表中.

I'm trying to make two types of users in laravel and I have two tables for that. I want to use defined register view and controller from laravel, but when I register new users it saved only in the first table.

问题出在这里<form class="form-horizontal" role="form" method="POST" action="{{ url('/register') }}" id="form_reg1">上的url('/register'),我不知道如何编辑此URL以发送帖子请求,因为我使用的是两种形式,一种用于第一类用户,一种第二种类型的用户.

The problem is with this url('/register') from here <form class="form-horizontal" role="form" method="POST" action="{{ url('/register') }}" id="form_reg1">which I don't know how to edit this url for sending post request, because I'm using two forms, one for the first type of user, and one for the second type of user.

这是第一个控制器:

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use Validator;
use App\Http\Controllers\Controller;
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 login / registration.
     *
     * @var string
     */
    protected $redirectTo = '/index';

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

    /**
     * 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, [
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|min:6|confirmed',
            'first_name' => 'required|max:255',
            'last_name' => 'required|max:255',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
            'first_name' => $data['first_name'],
            'last_name' => $data['last_name'],
        ]);
    }
}

这是第二个控制器:

<?php

namespace App\Http\Controllers\Auth;

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

class RegisterMerchantController 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 login / registration.
     *
     * @var string
     */
    protected $redirectTo = '/index';

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

    /**
     * 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, [
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|min:6|confirmed',
            'first_name' => 'required|max:255',
            'last_name' => 'required|max:255',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        return Merchant::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
            'first_name' => $data['first_name'],
            'last_name' => $data['last_name'],
        ]);
    }
}

有什么想法吗?

推荐答案

有几种方法可以解决此问题.其中之一是使用register.blade.php中的用户类型隐藏字段,并根据该字段在注册控制器中进行登录以验证输入.

There are a few ways to approach this problem. One of them is to use a user type hidden field in the register.blade.php and based on the field to have login in your register controller to validate the input.

<input type="hidden" name="user_type" id="user_type" value="user">

您可以使用

<select id="user_type" name="user_type">
  <option value="user">User</option>
  <option value="merchant">Merchant</option>
</select>

例如RegisterController:

protected function validator(array $data)
{
    return Validator::make($data, [
        'name' => 'required|max:255',
        'email' => 'required|email|max:255|unique:users',
        'password' => 'required|min:6|confirmed',
        'first_name' => 'required|max:255',
        'last_name' => 'required|max:255',
        'user_type' =>'required|in:user,merchant'
    ]);
}

/**
 * Create a new user instance after a valid registration.
 *
 * @param  array $data
 * @return User|Merchant
 */
protected function create(array $data)
{
    if ($data['user_type'] == "merchant") {
        return Merchant::create([
            'name'       => $data['name'],
            'email'      => $data['email'],
            'password'   => bcrypt($data['password']),
            'first_name' => $data['first_name'],
            'last_name'  => $data['last_name'],
        ]);
    } else {
        return User::create([
            'name'       => $data['name'],
            'email'      => $data['email'],
            'password'   => bcrypt($data['password']),
            'first_name' => $data['first_name'],
            'last_name'  => $data['last_name'],
        ]);
    }
}

这篇关于在Laravel 5.3中注册新类型的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆