php - Laravel 5.6,制作多个页面注册并将输入数据保存到 session

标签 php jquery mysql ajax laravel

我想有人给我一些建议。我真的卡住了。我是 laravel 的新手。我会尝试制作包含多个页面的注册页面。我想这样做:

  1. 第一页用户输入一些数据,然后点击next,数据会被存入session并重定向到第二页注册
  2. 之后,用户输入一些数据,当用户点击完成时,数据将被保存到用户表中。

我使用 laravel 身份验证 php artisan make:auth 并根据需要修改 View 和 Controller 。但是当我点击下一步按钮时,没有任何反应。

当我检查控制台时,它显示 POST http://localhost:8000/registrasi/v2 500 (Internal Server Error)

当我检查网络选项卡时,它说

"{,…}
exception
:
"Illuminate\Database\QueryException"
file
:
"C:\xampp\htdocs\vojo\vendor\laravel\framework\src\Illuminate\Database\Connection.php"
line
:
664
message
:
"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'senderEmail' in 'where clause' (SQL: select count(*) as aggregate from `users` where `senderEmail` = clubband@gmail.com)"
trace
:
[{file: "C:\xampp\htdocs\vojo\vendor\laravel\framework\src\Illuminate\Database\Connection.php",…},…]"

这是我的代码

网页.php

Route::get('/', function () {
return view('welcome');
});

Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
Route::group(['prefix'=>'registrasi'], function(){
Route::get('/v1','Auth\RegisterController@ShowView');
Route::post('/v2','Auth\RegisterController@store');
}); 

我的迁移用户表

    <?php

   use Illuminate\Support\Facades\Schema;
   use Illuminate\Database\Schema\Blueprint;
   use Illuminate\Database\Migrations\Migration;

   class CreateUsersTable extends Migration
   {
   /**
    * Run the migrations.
    *
    * @return void
    */
    public function up()
    {
       Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('username')->unique();
            $table->string('password');
            $table->string('email')->unique();
            $table->string('name');
            $table->enum('user_type', ['customer', 'band', 'admin'])- 
            >default('customer');
            $table->string('profile_picture')->nullable();
            $table->datetime('last_login')->nullable();
            $table->rememberToken();
            $table->timestamps();
            });
     }

     /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
     }
}

RegisterController.php

<?php

namespace App\Http\Controllers\Auth;
use Illuminate\Http\Request;
use App\Models\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Session;
class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/

use RegistersUsers;

/**
 * Where to redirect users after registration.
 *
 * @var string
 */
   protected $redirectTo = '/home';

/**
 * Create a new controller instance.
 *
 * @return void
 */
   public function __construct()
   {
        $this->middleware('guest');
    }

/**
 * Get a validator for an incoming registration request.
 *
 * @param  array  $data
 * @return \Illuminate\Contracts\Validation\Validator
 */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|string|max:255',
            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required|string|min:6|confirmed',
            ]);


     }

/**
 * Create a new user instance after a valid registration.
 *
 * @param  array  $data
 * @return \App\User
 */
     protected function create(array $data)
    {
         return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
             'password' => bcrypt($data['password']),
    ]);
}

protected function store(Request $request){
    $this->validate($request, [
        'senderName' => 'required|string|max:255',
        'senderEmail' => 'required|string|email|max:255|unique:users',
        'senderUsername' => 'required|string|max:255|unique:users',
        'senderPassword' => 'required|string|min:6'
    ]);

 Session::put('SimpanRegisterBand',$request->all());

    return view('layouts.test');
}
protected function ShowView(){
    return view('Auth.register');
}
}

对不起我的英语不好。谢谢。

最佳答案

如图所示的错误消息,你有一个SQL错误,这意味着你向不存在的表发送了一个字段“senderEmail”:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'senderEmail' in 'where clause' (SQL: select count(*) as aggregate from users where senderEmail = clubband@gmail.com)

其次,要在 session 中存储数据,请使用:

Session::put('key',$value);

要从 session 中获取数据,请使用:

Session::get('key')

不要忘记添加 use 行,例如:

use Session;

关于php - Laravel 5.6,制作多个页面注册并将输入数据保存到 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49965880/

相关文章:

javascript - ajax 调用缺少参数

javascript - 在 jQuery 中获取一个句子的多个 div 的开始和结束索引

PHP MySQL : Update incremental where condition can be duplicated

MySQL设计与关系

php - 从谷歌分析 api 获取数据的问题

php - Preg_replace 与数组替换

php - 设计基本站点时,我应该使用 PHP 吗?

php - 跨域Ajax数据

php - 用户输入的字符破坏了 xml_parse_into_struct 完成的 xml 解码

mysql - 如何使用 H2 测试 MySQL 区分大小写