dq13733519262 2016-08-30 12:06 采纳率: 0%
浏览 68

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

I am new to Laravel. I literally had to learn it last night. I implemented the Default Registration Form. I appended my own field and data and all is working fine. Now I'm having a challenge of passing a value from a hidden field in my RegistrationView to my RegisterController so it can be saved in the database. Basically I have two Registration Views of which that hidden field value will identify which registration Form it is

---This my 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;
    }


}

Now, the value I'm passing is to be binded to the 'package' column under my create query build. Thank you in advance.

  • 写回答

1条回答 默认 最新

  • dongying195959 2016-08-30 12:39
    关注

    The view passes all the fields including the hidden the same way. You haven't provided the view in your code so i guess you should manipulate it as the other form fields. You should change the following code:

    $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',
        ]);
    

    To this:

    $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'],
        ]);
    

    If this doesn't work try what i'm using in my projects:

    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();
    
    }
    

    To use Request you have to include in the top of your controller this use App\Http\Requests; The last approach is more clean to me. I hope i helped!

    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值