0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Laravelの認証機能をカスタマイズしてデータベースにレコードを作成する

Posted at

現状

Laravelの認証機能を有効化したけど、どうカスタマイズすればデータベースにレコードを登録できるんだろう…?

解決法

めちゃくちゃ簡単だった

(プロジェクト名)\app\Http\Controllers\Auth\RegisterController.phpから下記のコードをコピーして、挿入したいテーブル名、カラムとデータをコントローラーで指定するだけ。

RegisterController.php
protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
        ]);
    }

こんな感じに↓

CustomiseController.php
<?php

namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Table;
//(↑モデル宣言)
use Illuminate\Support\Facades\Hash;
//(↑パスワードなどハッシュ化するなら忘れないように)

class CustomiseController extends Controller
{
    public function customise(Request $request)
    {
    	$name = $request -> input('post_name');
    	$email = $request -> input('post_email');
    	$pass = $request -> input('post_pass');
    	$comment = $request -> input('post_comment');
    	$delete_flag = $request -> input('post_delete_flag');
    	Table::create([
            'name' => $name,
            'email' => $email,
            'password' => Hash::make($pass),
            'comment' => $comment,
            'delete_flag' => $delete_flag
        ]);
        return view('/home');
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?