我正在使用 fortify 創建和登錄用戶。因此,當我在資料庫和模型中添加一個新列“角色”時。注冊新用戶時遇到這種錯誤。不知何故,用戶資訊仍將被注冊并存盤在資料庫中,但是當我單擊注冊時顯示錯誤。我該如何解決 ?下面是我的代碼。
強化,CreateNewUser.php
<?php
namespace App\Actions\Fortify;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Laravel\Fortify\Contracts\CreatesNewUsers;
use Laravel\Jetstream\Jetstream;
class CreateNewUser implements CreatesNewUsers
{
use PasswordValidationRules;
/**
* Validate and create a newly registered user.
*
* @param array $input
* @return \App\Models\User
*/
public function create(array $input)
{
Validator::make($input, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => $this->passwordRules(),
'terms' => Jetstream::hasTermsAndPrivacyPolicyFeature() ? ['accepted', 'required'] : '',
])->validate();
$user = User::create([
'name' => $input['name'],
'email' => $input['email'],
'password' => Hash::make($input['password']),
'role' => 'user',
]);
}
}
用戶控制器
<?php
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function index()
{
$user = User::get();
return view('users.index', compact('user'));
}
}
用戶表
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('staff_id')->nullable();
$table->string('name');
$table->string('email')->unique();
$table->string('department')->nullable();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->string('role');
$table->rememberToken();
$table->foreignId('current_team_id')->nullable();
$table->string('profile_photo_path', 2048)->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
};
uj5u.com熱心網友回復:
您缺少函式內部的回傳。
$user = User::create([
'name' => $input['name'],
'email' => $input['email'],
'password' => Hash::make($input['password']),
'role' => 'user',
]);
return $user;
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/527103.html
標籤:php拉拉维尔
上一篇:paging:false不能在laravel上的資料表中使用“Bfrtip”,我如何知道資料表版本?
下一篇:什么會阻止本地化正常作業?
