Laravel将隐藏字段中的变量传递给RegisterController

时间:2016-08-30 12:06:16

标签: php laravel

我是Laravel的新手。我昨晚必须要学习它。我实施了默认注册表。我添加了自己的字段和数据,一切正常。现在我遇到了将RegisterView中隐藏字段的值传递给RegisterController的挑战,因此可以将其保存在数据库中。基本上我有两个注册视图,隐藏字段值将识别哪个注册表单

---这是我的RegisterController ---

<?php

namespace revolutions\Http\Controllers\Auth;

use revolutions\User;
use Validator;
use revolutions\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\RegistersUsers;
use Mail;
use Input;

use revolutions\Http\Controllers\SmsController;


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 = '/success';

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

  //Random number Generating Function
  public function generatePin( $number ) {
    // Generate set of alpha characters
    $alpha = array();
    for ($u = 65; $u <= 90; $u++) {
        // Uppercase Char
        array_push($alpha, chr($u));
    }

    // Just in case you need lower case
    // for ($l = 97; $l <= 122; $l++) {
    //    // Lowercase Char
    //    array_push($alpha, chr($l));
    // }

    // Get random alpha character
    $rand_alpha_key = array_rand($alpha);
    $rand_alpha = $alpha[$rand_alpha_key];

    // Add the other missing integers
    $rand = array($rand_alpha);
    for ($c = 0; $c < $number - 1; $c++) {
        array_push($rand, mt_rand(0, 9));
        shuffle($rand);
    }

    return implode('', $rand);
}

    /**
     * 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',
            'surname' => 'required|max:255',
            'cellphone' => 'required|max:10',
            'address' => 'required|max:100',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|min:6|confirmed',
            'bankname' => 'required|max:100',
            'accountnumber'=>'required|max:100',
            'holdername' => 'required|max:100',
            'branchcode' => 'required|max:10',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        $user = User::create([
          'reference_number' => $this->generatePin(13),
            'name' => $data['name'],
            'surname' => $data['surname'],
            'cellphone' => $data['cellphone'],
            'address' => $data['address'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
            'bankname' => $data['bankname'],
            'accountnumber' => $data['accountnumber'],
            'holdername' => $data['holdername'],
            'branchcode' => $data['branchcode'],
            'package'=>'bronze',
        ]);

          Mail::send('mail.registeremail', $data, function($message) use ($data)
             {
                 $message->from('admin@revo-lutions.com', "Revolutions");
                 $message->subject("Welcome to Revolutions");
                 $message->to($data['email']);
             });

             $mesg = "Welcome to Revolutions.Reference Number" . $this->generatePin(13);
$sms = new SmsController();

              $sms->sendSms($data['cellphone'],$mesg);


return $user;
    }


}

现在,我传递的值将被绑定到我的create query build下的'package'列。提前谢谢。

1 个答案:

答案 0 :(得分:0)

视图以相同的方式传递包括隐藏在内的所有字段。 您没有在代码中提供视图,因此我猜您应该将其作为其他表单字段进行操作。 您应该更改以下代码:

$user = User::create([
      'reference_number' => $this->generatePin(13),
        'name' => $data['name'],
        'surname' => $data['surname'],
        'cellphone' => $data['cellphone'],
        'address' => $data['address'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
        'bankname' => $data['bankname'],
        'accountnumber' => $data['accountnumber'],
        'holdername' => $data['holdername'],
        'branchcode' => $data['branchcode'],
        'package'=>'bronze',
    ]);

对此:

$user = User::create([
      'reference_number' => $this->generatePin(13),
        'name' => $data['name'],
        'surname' => $data['surname'],
        'cellphone' => $data['cellphone'],
        'address' => $data['address'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
        'bankname' => $data['bankname'],
        'accountnumber' => $data['accountnumber'],
        'holdername' => $data['holdername'],
        'branchcode' => $data['branchcode'],
        'package'=> $data['hiddenfieldname'],
    ]);

如果这不起作用,请尝试我在我的项目中使用的内容:

protected function create(Request $request)
{
    $input = $request->all();
    $user = new User;
    $user->name = $input['name'];
    $user->surname = $input['surname']; 
    ...
    $user->package = $input['hiddenfieldname'];  
    $user->save();

}

要使用请求,您必须包含在控制器的顶部 这个use App\Http\Requests; 最后一种方法对我来说更干净。我希望我帮忙!