laravel 如何验证表单输入并插入数据库
How to validate form input and insert into database in laravel
我正在尝试用 laravel 5.3 中的某些字段构建一个简单的注册表单,但在这样做时出现插入错误,以下是屏幕截图:
以下是我的 blade 文件:
@extends('admin.authlayouts')
@section('content')
<!-- Registration content -->
<div class="col-xs-12 login-content reg-content">
<span class="text-center">Register to get your account</span>
<div class="col-xs-12 login-form">
<form role="form" method="POST" action="{{ url('/memberregister') }}">
{{ csrf_field() }}
<label>Enter your personal details below.</label>
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<label class="sr-only" for="full-name">Full name</label>
<input type="text" name="name" id="name" value="{{ old('name') }}" placeholder="Full name" class="input-field full-name form-control" required autofocus>
@if ($errors->has('name'))
<span class="help-block">
<strong>{{ $errors->first('name') }}</strong>
</span>
@endif
</div>
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label class="sr-only" for="email">Email</label>
<input id="name" type="email" name="email" value="{{ old('email') }}" placeholder="Email" class="input-field email form-control" required>
@if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
</div>
<div class="form-group{{ $errors->has('address') ? ' has-error' : '' }}">
<label class="sr-only" for="address">Address</label>
<input type="text" name="address" id="address" value="{{ old('address') }}" placeholder="Address" class="input-field address form-control" required>
@if ($errors->has('address'))
<span class="help-block">
<strong>{{ $errors->first('address') }}</strong>
</span>
@endif
</div>
<div class="form-group{{ $errors->has('city') ? ' has-error' : '' }}">
<label class="sr-only" for="city">City/Town</label>
<input type="text" name="city" id="city" value="{{ old('city') }}" placeholder="City/Town" class="input-field city form-control" required>
@if ($errors->has('city'))
<span class="help-block">
<strong>{{ $errors->first('city') }}</strong>
</span>
@endif
</div>
<div class="form-group{{ $errors->has('country') ? ' has-error' : '' }}">
<label class="sr-only" for="country">Country</label>
<select name="country" id="country" class="input-field country form-control">
<option value="">Select a country</option>
<option value="India">India</option>
</select>
@if ($errors->has('country'))
<span class="help-block">
<strong>{{ $errors->first('country') }}</strong>
</span>
@endif
</div>
<label>Enter your new account details below.</label>
<div class="form-group{{ $errors->has('country') ? ' has-error' : '' }}">
<label class="sr-only" for="username">Username</label>
<input id="username" type="text" name="username" value="{{ old('city') }}" placeholder="Username" class="input-field username form-control">
@if ($errors->has('username'))
<span class="help-block">
<strong>{{ $errors->first('username') }}</strong>
</span>
@endif
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label class="sr-only" for="password">Password</label>
<input type="password" name="password" placeholder="Password" class="input-field password form-control">
@if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
@endif
</div>
<div class="form-group">
<label class="sr-only" for="retype-password">Confirm Password</label>
<input type="password" name="password_confirmation" id="password-confirm" placeholder="Retype Password" class="input-field re-password form-control">
</div>
<div class="form-group">
<label><input type="checkbox" name="" id="remember-check"> I agree to the Terms & Conditions & Privacy Policy</label>
</div>
<input type="submit" value="Sign up" class="btn btn-success pull-right">
</form>
</div>
</div>
@endsection
以下是我的控制器:
public function memberregister(Request $request)
{
$this->validate($request, User::$register_validation_rules);
$data = $request->only('name', 'email', 'password', 'username', 'address', 'city', 'is_admin', 'contact');
$data['password'] = bcrypt($data['password']);
$data['is_admin'] = 0;
$user = User::Create($data);
if($user){
\Auth::login($user);
return redirect('member.dashbaord');
}
return back()->withInput();
}
以下是我的用户模型:
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password', 'contact', 'address', 'city', 'country', 'username', 'bankname', 'bankaddress', 'ifsccode', 'accountnumber', 'is_admin'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public static $login_validation_rules = [
'username' => 'required|exists:user',
'password' => 'required'
];
public static $register_validation_rules = [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'username' => 'required|max:255|unique:users',
'password' => 'required|min:6|confirmed',
// 'contact_number' => 'required',
'address' => 'required|max:255',
'city' => 'required|max:255',
// 'state' => 'required|max:255',
// 'country' => 'required|max:255'
];
}
请帮帮我。
谢谢。
在您的 User 模型中,$fillable 数组包含 country 列。但是您正在尝试创建新记录而不为 county 列插入任何值。
因此,在 users table 中创建记录时,您必须为 country 列定义一个默认值。
在您的 users 迁移文件中,将 country 列设置为可为空。
$table->string('country')->nullable();
看起来您的架构中有 "country" 并且它不可为 nullable() and/or 没有 default(),并且您没有从表单中提供该值。适当修复(使其可为空,为其设置默认值,向表单添加国家/地区字段)。
我在你的 memberregister() 方法中根本没有看到对 "country" 的引用
检查以下行
$data = $request->only('name', 'email', 'password', 'username', 'address', 'city','country', 'is_admin', 'contact');
请求中没有国家。您还需要添加国家/地区。
我正在尝试用 laravel 5.3 中的某些字段构建一个简单的注册表单,但在这样做时出现插入错误,以下是屏幕截图:
以下是我的 blade 文件:
@extends('admin.authlayouts')
@section('content')
<!-- Registration content -->
<div class="col-xs-12 login-content reg-content">
<span class="text-center">Register to get your account</span>
<div class="col-xs-12 login-form">
<form role="form" method="POST" action="{{ url('/memberregister') }}">
{{ csrf_field() }}
<label>Enter your personal details below.</label>
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<label class="sr-only" for="full-name">Full name</label>
<input type="text" name="name" id="name" value="{{ old('name') }}" placeholder="Full name" class="input-field full-name form-control" required autofocus>
@if ($errors->has('name'))
<span class="help-block">
<strong>{{ $errors->first('name') }}</strong>
</span>
@endif
</div>
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label class="sr-only" for="email">Email</label>
<input id="name" type="email" name="email" value="{{ old('email') }}" placeholder="Email" class="input-field email form-control" required>
@if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
</div>
<div class="form-group{{ $errors->has('address') ? ' has-error' : '' }}">
<label class="sr-only" for="address">Address</label>
<input type="text" name="address" id="address" value="{{ old('address') }}" placeholder="Address" class="input-field address form-control" required>
@if ($errors->has('address'))
<span class="help-block">
<strong>{{ $errors->first('address') }}</strong>
</span>
@endif
</div>
<div class="form-group{{ $errors->has('city') ? ' has-error' : '' }}">
<label class="sr-only" for="city">City/Town</label>
<input type="text" name="city" id="city" value="{{ old('city') }}" placeholder="City/Town" class="input-field city form-control" required>
@if ($errors->has('city'))
<span class="help-block">
<strong>{{ $errors->first('city') }}</strong>
</span>
@endif
</div>
<div class="form-group{{ $errors->has('country') ? ' has-error' : '' }}">
<label class="sr-only" for="country">Country</label>
<select name="country" id="country" class="input-field country form-control">
<option value="">Select a country</option>
<option value="India">India</option>
</select>
@if ($errors->has('country'))
<span class="help-block">
<strong>{{ $errors->first('country') }}</strong>
</span>
@endif
</div>
<label>Enter your new account details below.</label>
<div class="form-group{{ $errors->has('country') ? ' has-error' : '' }}">
<label class="sr-only" for="username">Username</label>
<input id="username" type="text" name="username" value="{{ old('city') }}" placeholder="Username" class="input-field username form-control">
@if ($errors->has('username'))
<span class="help-block">
<strong>{{ $errors->first('username') }}</strong>
</span>
@endif
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label class="sr-only" for="password">Password</label>
<input type="password" name="password" placeholder="Password" class="input-field password form-control">
@if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
@endif
</div>
<div class="form-group">
<label class="sr-only" for="retype-password">Confirm Password</label>
<input type="password" name="password_confirmation" id="password-confirm" placeholder="Retype Password" class="input-field re-password form-control">
</div>
<div class="form-group">
<label><input type="checkbox" name="" id="remember-check"> I agree to the Terms & Conditions & Privacy Policy</label>
</div>
<input type="submit" value="Sign up" class="btn btn-success pull-right">
</form>
</div>
</div>
@endsection
以下是我的控制器:
public function memberregister(Request $request)
{
$this->validate($request, User::$register_validation_rules);
$data = $request->only('name', 'email', 'password', 'username', 'address', 'city', 'is_admin', 'contact');
$data['password'] = bcrypt($data['password']);
$data['is_admin'] = 0;
$user = User::Create($data);
if($user){
\Auth::login($user);
return redirect('member.dashbaord');
}
return back()->withInput();
}
以下是我的用户模型:
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password', 'contact', 'address', 'city', 'country', 'username', 'bankname', 'bankaddress', 'ifsccode', 'accountnumber', 'is_admin'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public static $login_validation_rules = [
'username' => 'required|exists:user',
'password' => 'required'
];
public static $register_validation_rules = [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'username' => 'required|max:255|unique:users',
'password' => 'required|min:6|confirmed',
// 'contact_number' => 'required',
'address' => 'required|max:255',
'city' => 'required|max:255',
// 'state' => 'required|max:255',
// 'country' => 'required|max:255'
];
}
请帮帮我。
谢谢。
在您的 User 模型中,$fillable 数组包含 country 列。但是您正在尝试创建新记录而不为 county 列插入任何值。
因此,在 users table 中创建记录时,您必须为 country 列定义一个默认值。
在您的 users 迁移文件中,将 country 列设置为可为空。
$table->string('country')->nullable();
看起来您的架构中有 "country" 并且它不可为 nullable() and/or 没有 default(),并且您没有从表单中提供该值。适当修复(使其可为空,为其设置默认值,向表单添加国家/地区字段)。
我在你的 memberregister() 方法中根本没有看到对 "country" 的引用
检查以下行
$data = $request->only('name', 'email', 'password', 'username', 'address', 'city','country', 'is_admin', 'contact');
请求中没有国家。您还需要添加国家/地区。