【发布时间】:2020-09-17 12:22:48
【问题描述】:
更新:问题已解决!
我正在更新我的 Laravel 技能(使用版本 7.13.0),遵循 freecodecamp.org 教程,此处:https://www.youtube.com/watch?v=ImtZ5yENzgE&t=11889s(1:21:49 到 1:22:50)。当我完成使用 php artisan tinker 手动添加配置文件的任务时,就像我在前端所做的那样,它无法保存。数据库是 sqlite。
这是完整的错误:
使用消息“SQLSTATE[23000] 照亮/数据库/查询异常: 完整性约束违规:19 NOT NULL 约束失败: profile.url (SQL: 插入 "profiles" ("title", "description", “user_id”、“updated_at”、“created_at”)值(酷标题, 说明, 1, 2020-05-29 18:41:02, 2020-05-29 18:41:02))'
我在 profiles_table.php 在 database\migrations 文件夹中的函数似乎没问题。是这样的:
public function up()
{
Schema::create('profiles', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->string('title')->nullable();
$table->text('description')->nullable();
$table->string('url')->nullable();
$table->timestamps();
$table->index('user_id');
});
}
Profile.php 模型 功能是:
public function user()
{
return $this->belongsTo(User::class);
}
而User.php模型的功能是:
public function profile()
{
return $this->hasOne(Profile::class);
}
所以,在终端中,输入php artisan tinker后,我填写了以下内容,然后保存失败:
>>> $profile = new \App\Profile();
=> App\Profile {#3056}
>>> $profile->title = 'Cool Title';
=> "Cool Title"
>>> $profile->description = 'Description';
=> "Description"
>>> $profile->user_id = 1;
=> 1
>>> $profile->save();
编辑:这是控制器文件ProfilesController.php,如果它有助于解决问题:
<?php
namespace App\Http\Controllers;
use \App\User;
use Illuminate\Http\Request;
class ProfilesController extends Controller
{
public function index($user)
{
$user = User::find($user);
return view('home', [
'user' => $user,
]);
}
}
所有部分都正常,在我输入 SAVE 后 - 它显示了上面的错误。因此我无法继续显示手动创建的新配置文件。
我在 Google 上搜索并在此处查找答案,例如 this question 和 that question。它没有解决我的问题,而且我认为其他问题不那么相关。
我应该怎么做才能修复它?
解决方案
感谢@mrhn,添加一个新的空表+安装composer require doctrine/dbal,如下所示:https://stackoverflow.com/a/37533819/12952748。
【问题讨论】:
-
退出tinker,再次迁移数据库,再次进入tinker重试
-
值得注意的是,您的代码没有问题,我无法重现错误复制面食
-
尝试将 URL 添加到模型中的可填充数组中。
-
萨利姆,我做了退出修补程序并再次进入,它没有改变。但是你是什么意思“代码没有问题”?在哪里展示东西? @TahaPaksu 在哪里添加 URL?在配置文件控制器返回?查一下,我加进去了。
标签: php mysql laravel sqlite tinker