Laravel - 注册不要在数据库中保存表单响应

时间:2017-06-28 07:15:39

标签: php database laravel laravel-5.4 blade

我试图做一个注册页面,但数据不会在我的数据库中发送。而且我没有任何错误。

这是控制器:(由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转储和模具不起作用,因此create()不是调用。并且我没有重定向aafter进入表单,它只是重新加载页面

我的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>&bull; Ajouter un utilisateur &bull;</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']
        ]);
    }

2 个答案:

答案 0 :(得分:1)

请参阅以下代码。

public function create(Request $request)
{
    $this->validate($request, [
           'nom' => 'required',
           'surname' => 'required',
            ]);
    $create = PostUser::create($request->all());
}

答案 1 :(得分:0)

好的,这是错误的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>&bull; Ajouter un utilisateur &bull;</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&#34; - &gt; all&#34;

event(new Registered($user = $this->create($request)));