Laravel - 注册不要在数据库中保存表单响应
Posted
技术标签:
【中文标题】Laravel - 注册不要在数据库中保存表单响应【英文标题】:Laravel - Register don't save form response in database 【发布时间】:2017-12-01 10:19:19 【问题描述】:我正在尝试注册页面,但数据未发送到我的数据库中。而且我没有任何错误。
这是控制器:(由 Laravel 生成)
namespace App\Http\Controllers\Auth;
use App\PostUser;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
class RegisterController extends Controller
protected $primaryKey = "id_biodiv_acteur";
protected $table = "acteur";
use RegistersUsers;
protected $redirectTo = '/pages/users';
public function __construct()
$this->middleware('guest');
protected function validator(array $data)
return Validator::make($data, [
'name' => 'required|string|max:255',
'surname' => 'string|max:255',
...
'picture' => 'image'
]);
protected function create(array $data)
dd($request->all());
return PostUser::create([
'nom' => $data['name'],
'prenom' => $data['surname'],
...
'image' => $data['picture']
]);
var 转储和 die 不起作用,因此 create() 不会被调用。而且我没有在输入表单后重定向,它只是重新加载页面
我的 register.blade.php :
<div class="add-content container">
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<h1>• Ajouter un utilisateur •</h1>
<div class="underline"></div>
<form action="!! route('register') !!" accept-charset="UTF-8" method="post" role="form">
!! csrf_field() !!
<div class="column-left">
<label class="has-float-label" for="name">
<input class="" type="text" placeholder="" name="name" required>
<span>Nom</span>
</label>
<label class="has-float-label" for="password">
<input class="" type="password" placeholder="" name="password" required>
<span>Mot de passe</span>
</label>
...
<label class="has-float-label" for="picture">
<input type="file" name="picture" multiple>
<span>Ajoutez des images</span>
</label>
</div>
<button type="submit" name="button">Enregistrer</button>
</form>
</div>
还有一个我添加到控制器的模型(PostUser.php):
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Auth\Events\Registered;
class PostUser extends Model
public $timestamps = false;
protected $primaryKey = "id_acteur";
protected $table = "acteur";
protected $fillable = [
'nom',
'prenom',
...
'image'
];
我对 create() 的最后一次更新:
protected function create(Request $request)
//dd($request->all());
$this->validate($request, [
'name' => 'required|string|max:255',
'surname' => 'string|max:255',
'password' => 'required|string|min:6|confirmed',
'country' => 'required|string|max:255',
'birthDate' => 'date',
'mobile' => 'required|string|max:45',
'email' => 'required|string|email|max:255|unique:acteur',
'city' => 'string|max:255',
'type' => 'string|max:45',
'numberMember' => 'string|max:255',
'acessNumber' => 'int|max:11',
'gender' => 'string|max:45',
'fax' => 'string|max:45',
'telephone' => 'string|max:45',
'adress' => 'string|max:255',
'postalCode' => 'string|max:16',
'motivation' => 'required|string|max:255',
'state' => 'string|max:45',
'picture' => 'image'
]);
return PostUser::create([
'nom' => $data['name'],
'prenom' => $data['surname'],
'mot_de_passe' => bcrypt($data['password']),
'pays' => $data['country'],
'date_naissance' => $data['birthDate'],
'mobile' => $data['mobile'],
'email' => $data['email'],
'ville' => $data['city'],
'type' => $data['type'],
'num_adherent' => $data['numberMember'],
'niveau_acces' => $data['accessNumber'],
'civilite' => $data['gender'],
'fax' => $data['fax'],
'telephone' => $data['telephone'],
'adresse' => $data['adress'],
'code_postal' => $data['postalCode'],
'raison_sociale' => $data['motivation'],
'statut' => $data['state'],
'image' => $data['picture']
]);
【问题讨论】:
您需要了解您正在使用use RegistersUsers;
这是特征。所以打开那个文件,看看你自己,事实上你确实有 register 和 showRegistrationForm 方法。
为什么你打电话给User::create
这个,因为你有PostUser
模型
哦,是的,抱歉我没看到
我修复了那个 Pankaj
【参考方案1】:
参考以下代码。
public function create(Request $request)
$this->validate($request, [
'nom' => 'required',
'surname' => 'required',
]);
$create = PostUser::create($request->all());
【讨论】:
好的,我改了名字,如何查看“acteur”的记录?使用 var_dump ? 我可以通过 phpmyadmin 查看是否有记录,我尝试在函数 create 中执行 var_dump 和 die 但什么也没发生,所以这个函数甚至没有被调用 Huuum,什么都没发生,我按照你说的做了。也许问题出在 html 中,因为我什至没有重定向到 /pages/users 我改变这个:Route::post('/create', ['as' => '/create', 'uses' => 'RegisterController@create']);在这个: $this->post('/create', ['as'=>'create','uses'=>'Auth\RegisterController@create']);在 Router.php 中。 dd 效果很好,我把结果放在问题中。 是的,但是这些数据没有被 validator() 验证?它需要通过验证()然后通过创建()【参考方案2】:好的,是 HTML 出错了,现在我的 html 看起来像这样:
@extends('layouts.master')
@section('content')
<div class="add-content container">
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<h1>• Ajouter un utilisateur •</h1>
<div class="underline"></div>
<form class="form-horizontal" role="form" method="POST" action=" route('register') " enctype="multipart/form-data">
csrf_field()
<div class="column-left">
<div class="form-group $errors->has('name') ? ' has-error' : '' ">
<label for="name" class="has-float-label">
<input id="name" type="text" class="" name="name" value=" old('name') " required autofocus>
<span>Nom</span>
</label>
@if ($errors->has('name'))
<span class="help-block">
<strong> $errors->first('name') </strong>
</span>
@endif
</div>
...
<div class="form-group $errors->has('picture') ? ' has-error' : '' ">
<label for="picture" class="has-float-label">
<input id="picture" multiple type="file" class="" name="picture" value=" old('picture') " required>
<span>Ajoutez des images</span>
</label>
@if ($errors->has('picture'))
<span class="help-block">
<strong> $errors->first('picture') </strong>
</span>
@endif
</div>
</div>
<button type="submit">Register</button>
</form>
</div>
@endsection
并且在 RegistersUsers.php 中,我在这一行的 $request 之后删除了"->all"
event(new Registered($user = $this->create($request)));
【讨论】:
以上是关于Laravel - 注册不要在数据库中保存表单响应的主要内容,如果未能解决你的问题,请参考以下文章