doubi9531 2017-09-25 07:25
浏览 103
已采纳

未定义的索引:RegisterController.php中的user_role

i want to save the radio button value to users database. but there is a ERROR:

Undefined index: user_role in RegisterController.php

may i know how to solve this error?

migration:

 Schema::table('users',function(Blueprint $table)
    {
        $table->integer('user_role');

    });

Register controller:

   protected function create(array $data)
{

    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
        'stuID'=> $data['stuID'],
        'user_role'=>$data['user_role'],
    ]);
}

blade file:

  <form class="form-horizontal" role="form" method="POST" action="{{ 
      route('register') }}">
                 {{ csrf_field() }}

 <label for="user_role" class="col-md-4 control-label  ">Role</label>

        <div class="user_role" id="user_role" data-toggle="buttons">
             <label class="btn btn-default user_role">
              <input type="radio" name="user_role" id="lecturer" value="1">
                    <span class="radio-dot"></span>
                      <span class="user_role-word">Lecturer</span>
             </label>

             <label class="btn btn-default user_role">
                <input type="radio" name="user_role" id="student"value="2">
                   <span class="radio-dot"></span>
                     <span class="user_role-word">Student</span>
              </label>
        </div>
  • 写回答

1条回答 默认 最新

  • duannaiying9662 2017-09-25 07:38
    关注

    If you won't select one of your radio buttons when submitting the form, you won't have $data['user_role'] index in the array. You can prevent this error by making radio buttons required in the form OR check the array for user_role index before use.

    return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
            'stuID'=> $data['stuID'],
            'user_role'=> isset($data['user_role']) ? $data['user_role'] : null
        ]);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化