Laravel表格数据不保存在neo4j图形数据库中 [英] Laravel form data not saving in neo4j graph DB

查看:193
本文介绍了Laravel表格数据不保存在neo4j图形数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个 laravel form ,它的屏幕截图如下:



我使用neo4j存储表单数据。

这里是代码:

app / views / duck-form.blade.php

 <!doctype html> 
< html>
< head>
< title> Laravel表单验证!< /标题>

<! - load bootstrap - >
< link rel =stylesheethref =// maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css\">
< style>
body {padding-bottom:40px;填充顶:40像素; }
< / style>
< / head>
< body class =container>

< div class =row>
< div class =col-sm-8 col-sm-offset-2>

< div class =page-header>
< h1>< span class =glyphicon glyphicon-flash>< / span>寄存器! < / H1>
< / div>

@if($ errors-> has())

@foreach($ errors-> all()as $ error)
{{$ error}}< br>
@endforeach
< / div>
@endif

<! - FORM STARTS HERE - >
< form method =POSTaction =/ ducknovalidate>

< div class =form-group @if($ errors-> has('name'))has-error @endif>
< label for =name>名称< / label>
< input type =textid =nameclass =form-controlname =nameplaceholder =输入您的姓名value ={{Input :: old('name') }}>
@if($ errors-> has('name'))< p class =help-block> {{$ errors-> first('name')}}< / p> ; @endif
< / div>
$ b $

< label for =email>电子邮件< / label>
< input type =textid =emailclass =form-controlname =emailplaceholder =输入您的电子邮件IDvalue ={{Input :: old )}}>
@if($ errors-> has('email'))< p class =help-block> {{$ errors-> first('email')}}< / p> ; @endif
< / div>
$ b< div class =form-group @if($ errors-> has('password'))has-error @endif>
< label for =password>密码< / label>
< input type =passwordid =passwordclass =form-controlname =password>
@if($ errors-> has('password'))< p class =help-block> {{$ errors-> first('password')}}< / p> ; @endif
< / div>

< div class =form-group @if($ errors-> has('password_confirm'))has-error @endif>
< label for =password_confirm>确认密码< / label>
< input type =passwordid =password_confirmclass =form-controlname =password_confirm>
@if($ errors-> has('password_confirm'))< p class =help-block> {{$ errors-> first('password_confirm')}}< / p> ; @endif
< / div>

< button type =submitclass =btn btn-success>提交< / button>

< / form>

< / div>
< / div>

< / body>
< / html>

1。我向 app / config / app.php 中的providers数组添加了'Artdarek\Neo4j\Neo4jServiceProvider'
2.我在 app / config / database.php中添加了 neo4j 配置

 'neo4j'=> [
'default'=> [
'host'=> 'localhost',
'port'=> 7474,
'username'=> null,
'password'=>空,
],
],



<3>。然后我为该表单添加了一个控制器:

 <?php 

class DuckController extends BaseController {

public function showWelcome()
{
return View :: make('duck');
}

}

4。这是我的 routes.php


$ b $

 <?php 

路由:: get('/',function()
{
return View :: make('hello');
});

//显示鸭子形式的路由
Route :: get('ducks',function()
{
return View :: make('duck-表格');
});

//处理鸭子的路由形式
Route :: post('ducks',array(''''=&';'csrf',function()
{

//创建验证规则------------------------
$ rules = array(
'name'=>'required',//只是一个普通的必需验证
'email'=>'required | email | unique:ducks',//必须且必须在鸭表中唯一
'password'=>'required',
'password_confirm'=>'required | same:password'//必须匹配密码字段
);

//创建自定义验证消息------------------
$ messages = array(
'required'=>'The:attribute ',
'same'=>'The:others must match。'
);

//做验证----- ------------------------ -----
//根据我们的表单输入验证
$ validator = Validator :: make(Input :: all(),$ rules,$ messages);

//检查验证器是否失败-----------------------
if($ validator-> failed ()){
//用错误消息重定向用户
$ messages = $ validator-> messages();

//也会将它们重新导向回旧的输入,所以它们不需要再次填写表单
//但是我们不会用输入的密码重定向它们

返回Redirect :: to('ducks')
- > withErrors($ validator)
- > withInput(Input :: except('password','password_confirm'));

} else {
//验证成功---------------------------

//我们的鸭子已通过所有测试!
//让他进入数据库

//为我们的鸭子创建数据
$ duck = new Duck;
$ duck-> name = Input :: get('name');
$ duck-> email = Input :: get('email');
$ duck-> password = Hash :: make(Input :: get('password'));

//保存我们的鸭子
$ duck-> save();

//重定向--------------------------------------- -
//将用户重定向到表单,以便他们可以重做
return Redirect :: to('ducks')
- > with('messages','万岁')!;

}

}));

5。这是我的 form 的模型文件:

 <?php 

class Duck扩展雄辩{

protected $ fillable = array('name','email','password');

}

6。这是我对 neo4j 的模型:

 <?php 

//使用Illuminate\Auth\EloquentUserProvider;

类数据库扩展Eloquent {

公共函数索引($ name,$ email,$ password,$ password_confirm){

$ formData = Neo4j :: makeNode();
$ formData-> setProperty('name',$ name)
- > setProperty('email',$ email)
- > setProperty('password',$ password)
- > setProprty('password_confirm',$ password_confirm)
- > save();

}

}

当我点击在那个提交按钮中,我得到这个错误:



[ 编辑 ]

我在工作就此:

这是我得到的新错误:



它是 csrf 令牌问题吗?

它指向:

  Route :: filter('csrf',function()
{
if(Session :: token()!= Input :: get('_ token'))
{
throw new Illuminate \Session\TokenMismatchException;
}
});

我无法解决它,因为5小时后数据没有储存在neo4j DB中。我该如何解决它? 使用 CSRF保护过滤器时,必须声明您的表单。 在你的刀片中:

  {{Form :: open(array('method'=&';'''''' action'=> URL :: to(/ ducks)))}} 

  {{Form :: close()}} 

这会在您的html中呈现与使用相同的表单:

  < form method =POSTaction ={{URL :: to(/ duck)}}> ...< / form> 

但也会添加隐藏的 _token 元素您缺少:

 < input type =hiddenname =_ tokenvalue =value_of_token/> 

希望有帮助!

编辑



或者,如果您不想重新创建< form> 你可以简单地使用:

$ p $ {{Form :: token()}}

您现有的< form> 标签内的某处可以创建它。

I wrote a laravel form, its screen shot is as given below:

And I use neo4j for storing that form data.
Here is the code:
app/views/duck-form.blade.php

<!doctype html>
<html>
<head>
    <title>Laravel Form Validation!</title>

    <!-- load bootstrap -->
    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
    <style>
        body    { padding-bottom:40px; padding-top:40px; }
    </style>
</head>
<body class="container">

<div class="row">
    <div class="col-sm-8 col-sm-offset-2">

        <div class="page-header">
            <h1><span class="glyphicon glyphicon-flash"></span> Register! </h1>
        </div>

        @if ($errors->has())
        <div class="alert alert-danger">
            @foreach ($errors->all() as $error)
                {{ $error }}<br>        
            @endforeach
        </div>
        @endif

        <!-- FORM STARTS HERE -->
        <form method="POST" action="/ducks" novalidate>

            <div class="form-group @if ($errors->has('name')) has-error @endif">
                <label for="name">Name</label>
                <input type="text" id="name" class="form-control" name="name" placeholder="Enter your name" value="{{ Input::old('name') }}">
                @if ($errors->has('name')) <p class="help-block">{{ $errors->first('name') }}</p> @endif
            </div>

            <div class="form-group @if ($errors->has('email')) has-error @endif">
                <label for="email">Email</label>
                <input type="text" id="email" class="form-control" name="email" placeholder="Enter your email id" value="{{ Input::old('email') }}">
                @if ($errors->has('email')) <p class="help-block">{{ $errors->first('email') }}</p> @endif
            </div>

            <div class="form-group @if ($errors->has('password')) has-error @endif">
                <label for="password">Password</label>
                <input type="password" id="password" class="form-control" name="password">
                @if ($errors->has('password')) <p class="help-block">{{ $errors->first('password') }}</p> @endif
            </div>

            <div class="form-group @if ($errors->has('password_confirm')) has-error @endif">
                <label for="password_confirm">Confirm Password</label>
                <input type="password" id="password_confirm" class="form-control" name="password_confirm">
                @if ($errors->has('password_confirm')) <p class="help-block">{{ $errors->first('password_confirm') }}</p> @endif
            </div>

            <button type="submit" class="btn btn-success">Submit</button>

        </form>

    </div>
</div>

</body>
</html>

1. I added 'Artdarek\Neo4j\Neo4jServiceProvider' to providers array in app/config/app.php.
2. I added neo4j configuration in app/config/database.php

 'neo4j' => [
            'default' => [
            'host'     => 'localhost',
            'port'     => 7474,
            'username' => null,
            'password' => null,
        ],
    ],

3. Then I added a controller for that form:

<?php

class DuckController extends BaseController {

    public function showWelcome()
    {
        return View::make('duck');
    }

}  

4. This is my routes.php.

<?php

Route::get('/', function()
{
    return View::make('hello');
});

// route to show the duck form
Route::get('ducks', function() 
{
    return View::make('duck-form');
});

// route to process the ducks form
Route::post('ducks', array('before' => 'csrf', function()
{

    // create the validation rules ------------------------
    $rules = array(
        'name'             => 'required',                       // just a normal required validation
        'email'            => 'required|email|unique:ducks',    // required and must be unique in the ducks table
        'password'         => 'required',
        'password_confirm' => 'required|same:password'          // required and has to match the password field
    );

    // create custom validation messages ------------------
    $messages = array(
        'required' => 'The :attribute is really really really important.',
        'same'  => 'The :others must match.'
    );

    // do the validation ----------------------------------
    // validate against the inputs from our form
    $validator = Validator::make(Input::all(), $rules, $messages);

    // check if the validator failed -----------------------
    if ($validator->fails()) {
        // redirect our user back with error messages       
        $messages = $validator->messages();

        // also redirect them back with old inputs so they dont have to fill out the form again
        // but we wont redirect them with the password they entered

        return Redirect::to('ducks')
            ->withErrors($validator)
            ->withInput(Input::except('password', 'password_confirm'));

    } else {
        // validation successful ---------------------------

        // our duck has passed all tests!
        // let him enter the database

        // create the data for our duck
        $duck = new Duck;
        $duck->name     = Input::get('name');
        $duck->email    = Input::get('email');
        $duck->password = Hash::make(Input::get('password'));

        // save our duck
        $duck->save();

        // redirect ----------------------------------------
        // redirect our user back to the form so they can do it all over again
        return Redirect::to('ducks')
            ->with('messages', 'Hooray!');

    }

}));

5. This is my model file for form:

<?php

class Duck extends Eloquent {

    protected $fillable = array('name', 'email', 'password');

}

6. This is my model for neo4j:

<?php

    //use Illuminate\Auth\EloquentUserProvider;

    class database extends Eloquent {

        public function index($name, $email, $password, $password_confirm) {

            $formData = Neo4j::makeNode();
            $formData->setProperty('name',$name)
                    ->setProperty('email',$email)
                    ->setProperty('password',$password)
                    ->setProprty('password_confirm',$password_confirm)
                    ->save();

        }

    }  

When I click on that submit button in that form, I get this error:

[ Edit ]
I was working on it:
This is the new error which I got:

Is it a csrf token issue?
It is pointing to:

Route::filter('csrf', function()
{
    if (Session::token() != Input::get('_token'))
    {
        throw new Illuminate\Session\TokenMismatchException;
    }
});

I'm unable to resolve it since 5 hours and data isn't getting stored in neo4j DB. How can I fix it?

解决方案

When using the CSRF Protection Filter, your form must be declared in your blade as such:

{{ Form::open(array('method' => 'POST', 'action' => URL::to("/ducks"))) }}

And closed:

{{ Form::close() }}

This will render the same form in your html as using:

<form method="POST" action="{{ URL::to("/ducks") }}">...</form>

But will also add the hidden _token element that you are missing:

<input type="hidden" name="_token" value="value_of_token"/>

Hope that helps!

Edit

Or, if you don't want to recreate your <form>, you may simply use:

{{ Form::token() }}

Somewhere inside your existing <form> tag to create it.

这篇关于Laravel表格数据不保存在neo4j图形数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆