在 Laravel 中,单选值列在数据库中为空

我的 Blade 文件有一个用户表单。我的代码运行良好,所有值都存储到数据库中,除了单选字段和单选列的值到数据库中说NULL。我阅读了许多论坛并相应地验证了我的代码,但仍然不知道我做错了什么。


刀片.php


<div class="form-group row">

<label for="user_type" class="col-md-4 col-form-label text-md-right ">{{ __('User Type') }}</label>


    <div class="col-md-6">

      <label class="radio-inline control-label">

      <input id="user_type" value="user" type="radio" class="form-control @error('user_type') is-invalid 

      @enderror" name="user_type" required autocomplete="user_type">User</label>


      <label class="radio-inline control-label">

      <input id="user_type" value="company" type="radio" class="form-control @error('user_type') is- 

      invalid @enderror" name="user_type" required autocomplete="user_type">Comapny</label>


        @error('user_type')

        <span class="invalid-feedback" role="alert">

          <strong>{{ $message }}</strong>

        <span>

        @enderror

   </div>

</div>

注册控制器.php


<?php


namespace App\Http\Controllers\Auth;

use \Illuminate\Http\Request;

use App\Http\Controllers\Controller;

use App\Providers\RouteServiceProvider;

use App\User;

use Illuminate\Foundation\Auth\RegistersUsers;

use Illuminate\Support\Facades\Hash;

use Illuminate\Support\Facades\Validator;

use Illuminate\Auth\Events\Registered;


class RegisterController extends Controller

{


    protected function validator(array $data)

    {

        $validator = Validator::make($data, [

            'register_name' => ['required', 'string', 'max:8'],

            'register_email' => ['required', 'string', 'email', 'max:255', 'unique:users,email'],

            'register_password' => ['required', 'string', 'min:8', 'confirmed'],

            'user_type'=> ['required','in:user,company'],


        ]);

        $validator->setAttributeNames([

            'register_email' => 'email',

            'register_password' => 'password',

        ]);


        return $validator;

    }


   


BIG阳
浏览 108回答 2
2回答

慕雪6442864

问题是您忘记将 user_type 放入用户模型的可填充数组中。将您的代码更改为以下将起作用。protected $fillable = [&nbsp; &nbsp; 'name', 'email', 'password', 'user_type'];

慕斯王

你应该为无线电设置名称并在控制器中使用:&nbsp; <label class="radio-inline control-label">&nbsp; <input name="user" id="user_type" value="user" type="radio" class="form-control @error('user_type') is-invalid&nbsp;&nbsp; @enderror" name="user_type" required autocomplete="user_type">User</label>&nbsp; <label class="radio-inline control-label">&nbsp; <input name="company" id="user_type" value="company" type="radio" class="form-control @error('user_type') is-&nbsp;&nbsp; invalid @enderror" name="user_type" required autocomplete="user_type">Comapny</label>&nbsp; &nbsp; @error('user_type')&nbsp; &nbsp; <span class="invalid-feedback" role="alert">&nbsp; &nbsp; &nbsp; <strong>{{ $message }}</strong>&nbsp; &nbsp; </span>&nbsp; &nbsp; @enderror在控制器中:类 RegisterController 扩展控制器 {protected function validator(array $data){&nbsp; &nbsp; $validator = Validator::make($data, [&nbsp; &nbsp; &nbsp; &nbsp; 'register_name' => ['required', 'string', 'max:8'],&nbsp; &nbsp; &nbsp; &nbsp; 'register_email' => ['required', 'string', 'email', 'max:255', 'unique:users,email'],&nbsp; &nbsp; &nbsp; &nbsp; 'register_password' => ['required', 'string', 'min:8', 'confirmed'],&nbsp; &nbsp; &nbsp; &nbsp; 'user_type' => ['required', 'in:user,company'],&nbsp; &nbsp; ]);&nbsp; &nbsp; $validator->setAttributeNames([&nbsp; &nbsp; &nbsp; &nbsp; 'register_email' => 'email',&nbsp; &nbsp; &nbsp; &nbsp; 'register_password' => 'password',&nbsp; &nbsp; ]);&nbsp; &nbsp; return $validator;}protected function create(array $data){&nbsp; &nbsp; if ($data['user']) {&nbsp; &nbsp; &nbsp; &nbsp; $user = User::create([&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'name' => $data['register_name'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'email' => $data['register_email'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'password' => Hash::make($data['register_password']),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'user_type' => $data['user'],&nbsp; &nbsp; &nbsp; &nbsp; ]);&nbsp; &nbsp; }&nbsp; &nbsp; if ($data['company']) {&nbsp; &nbsp; &nbsp; &nbsp; $user = User::create([&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'name' => $data['register_name'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'email' => $data['register_email'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'password' => Hash::make($data['register_password']),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'user_type' => $data['company'],&nbsp; &nbsp; &nbsp; &nbsp; ]);&nbsp; &nbsp; }&nbsp; &nbsp; $user->sendEmailVerificationNotification();&nbsp; &nbsp; return $user;}
打开App,查看更多内容
随时随地看视频慕课网APP