Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用Laravel将表单中的值存储在数据库中?_Php_Laravel - Fatal编程技术网

Php 如何使用Laravel将表单中的值存储在数据库中?

Php 如何使用Laravel将表单中的值存储在数据库中?,php,laravel,Php,Laravel,这是我的SubscriptionController代码: <?php namespace App\Http\Controllers; use Illuminate\Http\Request; class SubscribersController extends Controller { // //This method is to process the form public function postSubmit() { //we check if it

这是我的SubscriptionController代码:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class SubscribersController extends Controller
{
    //




//This method is to process the form
public function postSubmit() {

  //we check if it's really an AJAX request
  if(Request::ajax()) {

    $validation = Validator::make(Input::all(), array(
      //email field should be required, should be in an email//format, and should be unique
      'email' => 'required|email|unique:subscribers,email'
    )
    );

    if($validation->fails()) {
      return $validation->errors()->first();
    } else {

      $create = Subscribers::create(array(
        'email' => Input::get('email')
      ));

      if($create){
        return Redirect::to('/')
                       ->with('success','You have been successfully subscribe to us.');

      }else{
        echo 'We could not save your address to oursystem, please try again later';
      }

    }

  } else {
    return Redirect::to('subscribers');
  }
}
}
在路由/网络中:

Route::post('/subscribers', 'SubscribersController');
这是welcome.blade.php的代码

  <form action="/subscribers" method="post">

      <input type="email" name="email" placeholder="Email Address" required>

        <button type="button" class="btn btn-danger">Subscribe</button>

    </div>
  </form>

订阅
代码不会插入数据库,也不会在控制台中显示任何错误。如果出现错误且无任何结果,我将输入postsubmit函数echos。 我已经搜索了一个教程,但没有找到。
我是拉拉维尔的一名noob。

在您的路由文件中,您的路由需要发送到操作,而不仅仅是控制器

Route::post('/subscribers','SubscribersController')

应该是这个吗

Route::post('/subscribers','SubscribersController@postSubmit');

并将csrf字段添加到html表单中:

<form action="/subscribers" method="post">
    {{ csrf_field() }}
    <input type="email" name="email" placeholder="Email Address" required>
    <button type="button" class="btn btn-danger">Subscribe</button>
</form>

{{csrf_field()}}
订阅

在路由文件中,路由需要发送到操作,而不仅仅是控制器

Route::post('/subscribers','SubscribersController')

应该是这个吗

Route::post('/subscribers','SubscribersController@postSubmit');

并将csrf字段添加到html表单中:

<form action="/subscribers" method="post">
    {{ csrf_field() }}
    <input type="email" name="email" placeholder="Email Address" required>
    <button type="button" class="btn btn-danger">Subscribe</button>
</form>

{{csrf_field()}}
订阅

路由应该是
Route::post(“/subscribers”,”SubscribersController@postSubmit');
并将
csrf\u field()
添加到表单中

    <form action="/subscribers" method="post">
    {{csrf_field()}}
    <input type="email" name="email" placeholder="Email Address" required>
    <button type="button" class="btn btn-danger">Subscribe</button>
    </div>
   </form>`

{{csrf_field()}}
订阅
`

路由应该是
Route::post(“/subscribers”,”SubscribersController@postSubmit');
并将
csrf\u field()
添加到表单中

    <form action="/subscribers" method="post">
    {{csrf_field()}}
    <input type="email" name="email" placeholder="Email Address" required>
    <button type="button" class="btn btn-danger">Subscribe</button>
    </div>
   </form>`

{{csrf_field()}}
订阅
`

首先,您是否运行了:

php artisan migrate
第二,如上所述,您的路线:

Route::post('/subscribers', 'SubscribersController@postSubmit');
第三,在SubscriberController顶部,添加:

use Subscribers;
除了让CreateSubscriberTable扩展迁移之外,还需要另一个名为Subscribers的模型类来扩展模型。您可能需要的所有课程:

SubscribersController extends Controller (you have it)

CreateSubscribersTable extends Migration (you have this too)

Subscribers extends Model/Eloquent (Do you have this?)
对不起,这就是我能想到的。我将尝试查看更多帮助


或者,如果你真的很想找出问题出在哪里,试着在你的if-else中加入echo一些可能是“测试”的东西。

首先,你是否运行了:

php artisan migrate
第二,如上所述,您的路线:

Route::post('/subscribers', 'SubscribersController@postSubmit');
第三,在SubscriberController顶部,添加:

use Subscribers;
除了让CreateSubscriberTable扩展迁移之外,还需要另一个名为Subscribers的模型类来扩展模型。您可能需要的所有课程:

SubscribersController extends Controller (you have it)

CreateSubscribersTable extends Migration (you have this too)

Subscribers extends Model/Eloquent (Do you have this?)
对不起,这就是我能想到的。我将尝试查看更多帮助

或者,如果你真的很想找出问题出在哪里,试着在你的if-else中加入echo一些可能是“测试”的东西。

1)你的
迁移
内容是错误的

public function up()
{
    Schema::create('subscribers', function (Blueprint $table) { //** 'create', not 'table'
        $table->increments('id');
        $table->string('email,100)->default('');
        $table->timestamps();
    });
}
删除数据库内容并
php artisan迁移


2)您的表格是

<form action="/subscribers" method="post">
    {{ csrf_field() }}  //** add token
    <input type="email" name="email" placeholder="Email Address" required>
    <button type="submit" class="btn btn-danger">Subscribe</button> //** Button type : submit

</div>
</form>
你的控制器

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Subscribers;        //********* change this

class SubscribersController extends Controller
{

    //This method is to process the form
    public function postSubmit(Request $request) {

        $validation = Validator::make($request->all(), [
            //email field should be required, should be in an email format, and should be unique

            'email' => 'required|email|unique:subscribers,email',
        ]);

        if($validation->fails()) {
            return $validation->errors()->first();
        }

        $create = Subscribers::create(array(
            'email' => $request->email,
        ));

        return Redirect::to('/')->with('success','You have been successfully subscribe to us.');

    }
}
1)您的
迁移
内容错误

public function up()
{
    Schema::create('subscribers', function (Blueprint $table) { //** 'create', not 'table'
        $table->increments('id');
        $table->string('email,100)->default('');
        $table->timestamps();
    });
}
删除数据库内容并
php artisan迁移


2)您的表格是

<form action="/subscribers" method="post">
    {{ csrf_field() }}  //** add token
    <input type="email" name="email" placeholder="Email Address" required>
    <button type="submit" class="btn btn-danger">Subscribe</button> //** Button type : submit

</div>
</form>
你的控制器

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Subscribers;        //********* change this

class SubscribersController extends Controller
{

    //This method is to process the form
    public function postSubmit(Request $request) {

        $validation = Validator::make($request->all(), [
            //email field should be required, should be in an email format, and should be unique

            'email' => 'required|email|unique:subscribers,email',
        ]);

        if($validation->fails()) {
            return $validation->errors()->first();
        }

        $create = Subscribers::create(array(
            'email' => $request->email,
        ));

        return Redirect::to('/')->with('success','You have been successfully subscribe to us.');

    }
}


不显示任何错误,如果您在控制器中看到我的代码,这将显示错误,您能在此过程中帮助我吗@Hollings?检查
/storage/log/
文件夹中
laravel.log
文件的结尾,查看您的回音可能不显示的错误。不显示任何内容,您能检查语法是否正确吗?@nodejscodrd请查看我对我的帖子的编辑,内容是关于添加
{csrf_field()}
不工作。。。如果我收到任何请求,我已经在选项卡网络中看到了。但没什么。。。不要发送表格。。。是“堆栈”不显示任何错误,如果你在控制器中看到我的代码,这将显示错误,你能在这个过程中帮助我吗@Hollings?检查
/storage/log/
文件夹中
laravel.log
文件的结尾,查看您的回音可能不显示的错误。不显示任何内容,您能检查语法是否正确吗?@nodejscodrd请查看我对我的帖子的编辑,内容是关于添加
{csrf_field()}
不工作。。。如果我收到任何请求,我已经在选项卡网络中看到了。但没什么。。。不要发送表格。。。“stack”@Norrris Oduro不起作用。。。请查看您的答案。@nodejscdrd将按钮类型更改为提交。(1/1)在SubscribersController.php(第26行)中找不到FatalErrorException类“订阅者”
“电子邮件”=>“必需的”|电子邮件|唯一的:订阅者,电子邮件“,
在验证规则中改为
“电子邮件”=>“必需的”|电子邮件|唯一的:订阅者”,
SubscribersController.php(第27行)中找不到类“订阅者”并不是错误@Norrris Oduro不工作。。。请查看您的答案。@nodejscdrd将按钮类型更改为提交。(1/1)在SubscribersController.php(第26行)中找不到FatalErrorException类“订阅者”
“电子邮件”=>“必需的”|电子邮件|唯一的:订阅者,电子邮件“,
在验证规则中改为
“电子邮件”=>“必需的”|电子邮件|唯一的:订阅者”,
SubscribersController.php(第27行)(1/1)中找不到类“Subscribers”(第26行)(1/1)SubscribersController.php中找不到FatalErrorException类“Subscribers”(第26行)(1/1)中找不到FatalErrorException类“Subscribers”(第26行)在SubscribersController.php(第26行)中找不到FatalErrorException类“subscribers”,类“subscribers”未找到是指使用$create=subscribers::create(数组('email'=>$request->email,))。它声明该类不存在。你确定你有那种型号吗?是的,您有CreateSubscribersTable,但这是您的迁移,而不是实际的模型。您必须有另一个名为Subscribers extends Model的类。(1/1)在SubscribersController.php(第26行)中找不到FatalErrorException类“Subscribers”未找到是指使用$create=Subscribers::create(数组('email'=>$request->email,))。它声明该类不存在。你确定你有那种型号吗?是的,你有那张桌子