データベースにSqliteを用いてユーザー登録機能を作ろうとしてます。
データベースに値を追加しメインページにリダイレクトされるように作ったのですが、リダイレクトされません。
sqliteに接続しUserテーブルの中身を確認しても値が登録されていません。
特にエラーも出ておらず、どこが原因のなのかわからないです。
LaravelとVueを使って作ってます。
どうかよろしくお願いします。
ユーザー登録のライブラリでvee-validateを使っています。
OS:Mac M1
Register.vue
<template> <div> <div cclass="container"> <div class="notification"> <div class="columns"> <div class="column"> <div class="panel"> <div class="panel-heading"> <h2 class="title is-4">新規登録</h2> </div> <div class="panel-body"> <ValidationObserver class="form-horizontal" ref="observer" action="/register" id="register" method="post" tag="form" @submit.prevent="register()" v-slot="{ invalid }" > <input type="hidden" name="_token" :value="csrf" /> <div class="form-group"> <div class="field"> <label for="name" class="column">名前</label> <validation-provider name="名前" rules="required|max:20" v-slot="{ errors }"> <input v-model="name" class="input is-success" type="text" placeholder="名前" /> </validation-provider> </div> </div> <div class="form-group"> <div class="field"> <label for="email" class="column">メール</label> <validation-provider name="メールアドレス" rules="required|email" v-slot="{ errors }" > <input v-model="email" class="input is-success" type="text" placeholder="メールアドレス" /> <div class="alert alert-danger" v-show="errors[0]">{{ errors[0] }}</div> </validation-provider> </div> </div> <div class="field is-grouped"> <div class="control"> <button type="submit" class="button is-link">登録する</button> </div> </div> </ValidationObserver> </div> </div> </div> </div> </div> </div> </div> </template> <script> import {ValidationProvider, ValidationObserver, extend } from "vee-validate"; import { required, max, min, email, confirmed } from "vee-validate/dist/rules"; extend("required", { ...required, message: "{_field_}は必須です" }); extend("email", { ...email, message: "{_field_}はメールアドレス形式で入力してください" }); extend("min", { ...min, message: "{_field_}は最低でも{length}文字入力してください" }); export default { components: { ValidationProvider, ValidationObserver }, data() { return { name: "", email: "", csrf: document .querySelector('meta[name="csrf-token"]') .getAttribute("content"), }; }, methods: { async register() { const isValid = await this.$refs.observer.validate(); if (isValid) { document.querySelector("#register").submit(); } } } }; </script>
RegisteController.php
php
1<?php 2 3namespace App\Http\Controllers\Auth; 4 5use App\Http\Controllers\Controller; 6use App\Providers\RouteServiceProvider; 7use App\User; 8use Illuminate\Foundation\Auth\RegistersUsers; 9use Illuminate\Support\Facades\Hash; 10use Illuminate\Support\Facades\Validator; 11 12 13 14class RegisterController extends Controller 15{ 16 /* 17 |-------------------------------------------------------------------------- 18 | Register Controller 19 |-------------------------------------------------------------------------- 20 | 21 | This controller handles the registration of new users as well as their 22 | validation and creation. By default this controller uses a trait to 23 | provide this functionality without requiring any additional code. 24 | 25 */ 26 27 use RegistersUsers; 28 29 /** 30 * Where to redirect users after registration. 31 * 32 * @var string 33 */ 34 35 36 protected $redirectTo = '/'; 37 38 39 /** 40 * Create a new controller instance. 41 * 42 * @return void 43 */ 44 public function __construct() 45 { 46 $this->middleware('guest'); 47 } 48 49 /** 50 * Get a validator for an incoming registration request. 51 * 52 * @param array $data 53 * @return \Illuminate\Contracts\Validation\Validator 54 */ 55 protected function validator(array $data) 56 { 57 return Validator::make($data, [ 58 'name' => ['required', 'string', 'max:255'], 59 'email' => ['required', 'string', 'email', 'max:255', 'unique:users'], 60 'password' => ['required', 'string', 'min:8', 'confirmed'], 61 ]); 62 } 63 64 /** 65 * Create a new user instance after a valid registration. 66 * 67 * @param array $data 68 * @return \App\User 69 */ 70 protected function create(array $data) 71 { 72 return User::create([ 73 'name' => $data['name'], 74 'email' => $data['email'], 75 'password' => Hash::make($data['password']), 76 ]); 77 } 78}
Userテーブル
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateUsersTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('users', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('name'); $table->string('email')->unique(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('users'); } }