gpt4 book ai didi

php - 在registrationController laravel中提交用户表单时出现404错误

转载 作者:行者123 更新时间:2023-12-03 11:22:51 25 4
gpt4 key购买 nike

在注册期间尝试将用户详细信息插入多个表时出现 404 错误
我的用户模型:

<?php

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Hash;


class User extends Authenticatable
{
use Notifiable;
use HasRoles;

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'username','accno', 'email', 'password', 'role', 'status', 'activation_code'
];

protected $guarded = [];

/**
* 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',
];

// a mutator for the email attribute of our model with email validation check and check to avoid duplicate email entries.

protected $table = 'users';

public $timestamps = false;

public $incrementing = false;

public function setEmailAttribute($email)
{
// Ensure valid email
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
throw new \Exception("Invalid email address.");
}

// Ensure email does not exist
elseif (static::whereEmail($email)->count() > 0) {
throw new \Exception("Email already exists.");
}

$this->attributes['email'] = $email;
}

public function setPasswordAttribute($password)
{
$this->attributes['password'] = Hash::make($password);
}

public function profiles()
{
return $this->hasOne(profiles::class);
}

public function accounts()
{
return $this->hasOne(accounts::class);
}

public function transactions()
{
return $this->hasMany(transactions::class);
}
}
我尝试通过使用 RegisterUserTrait 将验证代码与逻辑分离来进行重构
<?php

namespace App\Traits;

use App\User;
use App\Profile;
use App\Account;
use Keygen;

trait RegisterUser
{
public function registerUser($fields)
{


$user = User::create([

'username' => $fields->username,
'accno' => $this->generateAccountNumber(),
'email' => $fields->email,
'password' => $fields->password = bcrypt(request('password')),
'roles' => $fields->roles,
'activation_code' => $this->generateToken()
]);

Profile::create([
'accno' => $user->accno,
'username' => $user->username,
'acc_type' => $fields->acc_type,
'firstname' => $fields->firstname,
'lastname' => $fields->lastname,
'nationality' => $fields->nationality,
'occupation' => $fields->occupation,
'address' => $fields->address,
'city' => $fields->city,
'state' => $fields->state,
'zipcode' => $fields->zipcode,
'phoneno' => $fields->phoneno,
'dob' => $fields->dob,
'gender' => $fields->gender,
'martial_status' => $fields->martial_status,
'user_image' => $fields->user_image,
]);

Account::create([
'accno' => $user->accno,
'username' => $user->username,
]);

return $user;

}
然后使用我的 registrationController 存储数据:
<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use App\Http\Requests\RegistrationRequest;
use App\Traits\RegisterUser;

class RegistrationController extends Controller
{
use RegisterUser;

public function show()
{
return view('auth/register');
}

public function register(RegistrationRequest $requestFields)
{
//calling the registerUser method inside RegisterUser trait.
$user = $this->registerUser($requestFields);

return redirect('/login');


}
}

但是当我注册用户时,数据只保存在 create_user_table 中并返回 404 page not found 错误。如何将数据保存到选定的表并重定向到登录页面?

最佳答案

正如我所看到的,这对于用户模型中的外键关系而言并非如此

public function profiles()
{
return $this->hasOne(profiles::class);
}

public function accounts()
{
return $this->hasOne(accounts::class);
}

public function transactions()
{
return $this->hasMany(transactions::class);
}

它应该如下;
public function profiles()
{
return $this->hasOne(Profile::class);
}

public function accounts()
{
return $this->hasOne(Account::class);
}

public function transactions()
{
return $this->hasMany(Transaction::class);
}

关于php - 在registrationController laravel中提交用户表单时出现404错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61133917/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com