当我尝试注册新用户时出现此错误。 '部门 ID 必须是整数。'
Posted
技术标签:
【中文标题】当我尝试注册新用户时出现此错误。 \'部门 ID 必须是整数。\'【英文标题】:I get this error when I try to register a new user. 'The department id must be an integer.'当我尝试注册新用户时出现此错误。 '部门 ID 必须是整数。' 【发布时间】:2021-02-19 05:48:53 【问题描述】:我在尝试注册新用户时收到此错误:“部门 ID 必须是整数”。我收到的所有答案都很有帮助,只是我有时会感到困惑。因此,我添加了更多代码以进行澄清。我已经争吵了几个小时了。这是迁移文件。
Schema::create('departments', function (Blueprint $table)
$table->bigIncrements('department_id');
$table->integer('department_name');
$table->integer('department_code')->unique();
$table->text('department_discription')->nullable();
$table->tinyInteger('department_status')->default(1);
$table->softDeletes();
$table->timestamps();
这是部门控制器代码
<?php
namespace App\Http\Controllers;
use App\Http\Requests\CreateDepartmentRequest;
use App\Http\Requests\UpdateDepartmentRequest;
use App\Repositories\DepartmentRepository;
use App\Http\Controllers\AppBaseController;
use Illuminate\Http\Request;
use Flash;
use Response;
class DepartmentController extends AppBaseController
/** @var DepartmentRepository */
private $departmentRepository;
public function __construct(DepartmentRepository $departmentRepo)
$this->departmentRepository = $departmentRepo;
/**
* Display a listing of the Department.
*
* @param Request $request
*
* @return Response
*/
public function index(Request $request)
$departments = $this->departmentRepository->all();
return view('departments.index')
->with('departments', $departments);
/**
* Show the form for creating a new Department.
*
* @return Response
*/
public function create()
return view('departments.create');
/**
* Store a newly created Department in storage.
*
* @param CreateDepartmentRequest $request
*
* @return Response
*/
public function store(CreateDepartmentRequest $request)
$input = $request->all();
$department = $this->departmentRepository->create($input);
Flash::success('Department saved successfully.');
return redirect(route('departments.index'));
/**
* Display the specified Department.
*
* @param int $id
*
* @return Response
*/
public function show($id)
$department = $this->departmentRepository->find($id);
if (empty($department))
Flash::error('Department not found');
return redirect(route('departments.index'));
return view('departments.show')->with('department', $department);
/**
* Show the form for editing the specified Department.
*
* @param int $id
*
* @return Response
*/
public function edit($id)
$department = $this->departmentRepository->find($id);
if (empty($department))
Flash::error('Department not found');
return redirect(route('departments.index'));
return view('departments.edit')->with('department', $department);
/**
* Update the specified Department in storage.
*
* @param int $id
* @param UpdateDepartmentRequest $request
*
* @return Response
*/
public function update($id, UpdateDepartmentRequest $request)
$department = $this->departmentRepository->find($id);
if (empty($department))
Flash::error('Department not found');
return redirect(route('departments.index'));
$department = $this->departmentRepository->update($request->all(), $id);
Flash::success('Department updated successfully.');
return redirect(route('departments.index'));
/**
* Remove the specified Department from storage.
*
* @param int $id
*
* @throws \Exception
*
* @return Response
*/
public function destroy($id)
$department = $this->departmentRepository->find($id);
if (empty($department))
Flash::error('Department not found');
return redirect(route('departments.index'));
$this->departmentRepository->delete($id);
Flash::success('Department deleted successfully.');
return redirect(route('departments.index'));
这是部门代码
<?php
namespace App\Models;
use Eloquent as Model;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* Class Departments
* @package App\Models
* @version September 21, 2020, 4:31 pm UTC
*
* @property integer $department_name
* @property integer $department_code
* @property string $department_discription
* @property boolean $department_status
*/
class Departments extends Model
use SoftDeletes;
public $table = 'departments';
const CREATED_AT = 'created_at';
const UPDATED_AT = 'updated_at';
protected $dates = ['deleted_at'];
public $fillable = [
'department_name',
'department_code',
'department_discription',
'department_status'
];
/**
* The attributes that should be casted to native types.
*
* @var array
*/
protected $casts = [
'department_id' => 'integer',
'department_name' => 'integer',
'department_code' => 'integer',
'department_discription' => 'string',
'department_status' => 'boolean'
];
/**
* Validation rules
*
* @var array
*/
public static $rules = [
'department_name' => 'required|integer',
'department_code' => 'required|integer',
'department_discription' => 'nullable|string',
'department_status' => 'required|boolean',
'deleted_at' => 'nullable',
'created_at' => 'nullable',
'updated_at' => 'nullable'
];
【问题讨论】:
$table->bigIncrements('id');
或 $table->id();
自动递增 UNSIGNED BIGINT(主键)等效列。
验证似乎失败了
请提供导致错误的代码、注册、用户模型和迁移
【参考方案1】:
在您的迁移中,尝试将 'department_id' 设为主键 declaratively:
$table->primary('department_id');
然后(就像评论中的 sta 所说)在您的部门模型中:
protected $primaryKey = "department_id";
或将其名称更改为“id”
【讨论】:
【参考方案2】:你应该像这样创建一个部门:
\App\Models\Department::create([
'department_name' => 1,
'department_code' => 1,
'department_discription' => 'first department',
'department_status' => 1
]);
不要忘记将列名添加到部门模型中的可填充变量
【讨论】:
这里是部门模型 public $fillable = [ 'department_name', 'department_code', 'department_discription', 'department_status' department_id 列是“自动增量”,您是否为此分配了任何值?为了更清楚,请完整分享您的代码。【参考方案3】:如果我理解您的问题,您将需要更改数据库中的 department_id 字段。 创建一个新的迁移并为您的 department_id 键设置它。
$table->unsignedBigInteger('department_id')->autoIncrement();
这应该可以解决您的问题。它会做的是在您需要添加关系的情况下维护数据库的完整性,因为它将创建与其他表上的 id 相同类型的字段 (unsignedBigInteger),但它也会自动增加该字段。
【讨论】:
以上是关于当我尝试注册新用户时出现此错误。 '部门 ID 必须是整数。'的主要内容,如果未能解决你的问题,请参考以下文章
如何处理 android.view.InflateException?当我尝试打开地图时出现此错误
为啥当我尝试在 MariaDB 数据库上创建此函数(使用点数据类型)时出现此错误?
当我尝试使用 createReadStream 通过 apollo-server 上传文件时出现此错误,
在 FOSUserBundle 中通过 post 方法注册新用户时禁用 CSRF
当我尝试将 TapGestureRecognizer 添加到我的 UIImageView 时出现此错误:Unrecognized selector sent to class