我用Laravel 8开始了一个新项目。我用的是Laravel Breeze的首发阵容。但我无法自定义字段。我已经更改了迁移和注册控制器和用户模型中的字段。
这是我的代码:
迁移文件。
<?php
use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;
class TblUsers extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('tbl_users', function (Blueprint $table) {
$table->id();
$table->string('fullname');
$table->string('username');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('phone');
$table->string('organization_type');
$table->string('community_dev_auth_id');
$table->string('file_names');
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('tbl_users');
}
}
寄存器控制器文件。
<?php
namespace AppHttpControllersAuth;
use AppHttpControllersController;
use AppModelsUser;
use AppProvidersRouteServiceProvider;
use IlluminateAuthEventsRegistered;
use IlluminateHttpRequest;
use IlluminateSupportFacadesAuth;
use IlluminateSupportFacadesHash;
class RegisteredUserController extends Controller
{
/**
* Display the registration view.
*
* @return IlluminateViewView
*/
public function create()
{
return view('auth.register');
}
/**
* Handle an incoming registration request.
*
* @param IlluminateHttpRequest $request
* @return IlluminateHttpRedirectResponse
*
* @throws IlluminateValidationValidationException
*/
public function store(Request $request)
{
$request->validate([
'fullname' => 'required|string|max:255',
'username' => 'required|string|max:255',
'email' => 'required|senter code heretring|email|max:255|unique:users',
'phone' => 'required|string|max:255',
'organization' => 'required|string|max:255',
'community' => 'required|string|max:255',
// 'phone' => 'required|string|max:255',
'password' => 'required|string|min:8',
]);
Auth::login($user = User::create([
'fullname' => $request->fullname,
'username' => $request->username,
'email' => $request->email,
'phone' => $request->phone,
'organization_type' => $request->organization,
'community_dev_auth_id' => $request->community,
'password' => Hash::make($request->password),
]));
event(new Registered($user));
return redirect(RouteServiceProvider::HOME);
}
}
用户模型文件。
<?php
namespace AppModels;
use IlluminateContractsAuthMustVerifyEmail;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateFoundationAuthUser as Authenticatable;
use IlluminateNotificationsNotifiable;
class User extends Authenticatable
{
use HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'fullname',
'username',
'email',
'phone',
'organization',
'community',
'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
我已经运行了这个项目,但它返回了这个错误:
SQLSTATE[42S02]:找不到基表或视图:1146表"ambulance_dubai.users"不存在(SQL:选择count(*)作为来自
users
的聚合,其中
由于用户模型使用不同的表名,因此必须在模型中定义它。默认情况下,Laravel将查找复数名称如果您的模型没有表属性,则为模型(users
)的。
将其添加到用户模型中:
protected $table='tbl_user';
另一个原因可能是验证对表有不同的名称。例如,在验证中使用表tbl_users
可能存在错误,并具有:'required|unique:tbl_user,email'
。
字母";s";缺少,将引发错误。
`
return [
//
'name' => 'required',
'image' => 'required',
'email' => 'required|unique:users,email',
'username' => 'required|unique:users,username',
'password' => 'required|min:8',
];
`
您看到的用户应该与数据库中的表名相对应。这正在验证中。
首先,您将检查用户表是否已迁移,用户表是否未迁移使用该命令
php artisan migrate
打开User Model
并添加$table
class User extends Authenticatable {
protected $table = 'users';
}