我无法弄清楚如何使用Laravel框架将新列添加到现有数据库表中.
我尝试使用...编辑迁移文件
<?php
public function up()
{
Schema::create('users', function ($table) {
$table->integer("paid");
});
}
Run Code Online (Sandbox Code Playgroud)
在终端,我执行php artisan migrate:install和migrate.
如何添加新列?
我用unsigned创建了一个迁移user_id.如何user_id在新迁移中编辑以进行编辑nullable()?
Schema::create('throttle', function(Blueprint $table)
{
$table->increments('id');
// this needs to also be nullable, how should the next migration be?
$table->integer('user_id')->unsigned();
}
Run Code Online (Sandbox Code Playgroud) 我刚刚开始使用Laravel,我收到以下错误:
未知列'updated_at'插入gebruikers(naam,wachtwoord,updated_at,created_at)
我知道错误来自迁移表时的时间戳列,但我没有使用该updated_at字段.我曾经使用它,当我按照Laravel教程,但现在我正在制作(或试图制作)我自己的东西.即使我不使用时间戳,我也会收到此错误.我似乎无法找到它被使用的地方.这是代码:
调节器
public function created()
{
if (!User::isValidRegister(Input::all())) {
return Redirect::back()->withInput()->withErrors(User::$errors);
}
// Register the new user or whatever.
$user = new User;
$user->naam = Input::get('naam');
$user->wachtwoord = Hash::make(Input::get('password'));
$user->save();
return Redirect::to('/users');
}
Run Code Online (Sandbox Code Playgroud)
路线
Route::get('created', 'UserController@created');
Run Code Online (Sandbox Code Playgroud)
模型
public static $rules_register = [
'naam' => 'unique:gebruikers,naam'
];
public static $errors;
protected $table = 'gebruikers';
public static function isValidRegister($data)
{
$validation = Validator::make($data, static::$rules_register);
if ($validation->passes()) {
return true;
}
static::$errors = $validation->messages();
return false;
}
Run Code Online (Sandbox Code Playgroud)
我一定是忘记了什么......我在这里做错了什么?
无法弄清楚如何在Laravel中的表上设置正确的onDelete约束.(我和SqLite合作)
$table->...->onDelete('cascade'); // works
$table->...->onDelete('null || set null'); // neither of them work
Run Code Online (Sandbox Code Playgroud)
我有3次迁移,创建了gallery表:
Schema::create('galleries', function($table)
{
$table->increments('id');
$table->string('name')->unique();
$table->text('path')->unique();
$table->text('description')->nullable();
$table->timestamps();
$table->engine = 'InnoDB';
});
Run Code Online (Sandbox Code Playgroud)
创建图片表:
Schema::create('pictures', function($table)
{
$table->increments('id');
$table->text('path');
$table->string('title')->nullable();
$table->text('description')->nullable();
$table->integer('gallery_id')->unsigned();
$table->foreign('gallery_id')
->references('id')->on('galleries')
->onDelete('cascade');
$table->timestamps();
$table->engine = 'InnoDB';
});
Run Code Online (Sandbox Code Playgroud)
将图库表链接到图片:
Schema::table('galleries', function($table)
{
// id of a picture that is used as cover for a gallery
$table->integer('picture_id')->after('description')
->unsigned()->nullable();
$table->foreign('picture_id')
->references('id')->on('pictures')
->onDelete('cascade || set null || null'); // neither of them works
});
Run Code Online (Sandbox Code Playgroud)
我没有收到任何错误.此外,即使"级联"选项也不起作用(仅在图库表上).删除图库会删除所有图片.但删除封面图片,不会删除图库(用于测试目的).
由于即使"级联"未被触发,我"设置空"也不是问题.
编辑(解决方法):
阅读完这篇 …
我正在迁移中将软删除列添加到我的表中:
public function up()
{
Schema::table("users", function ($table) {
$table->softDeletes();
});
}
Run Code Online (Sandbox Code Playgroud)
但是,down()如果我回滚迁移,如何在我的函数中删除这些?是否有内置方法来执行此操作,或者我只是手动删除添加的列?
我的团队的开发人员已经习惯了Laravel迁移的强大功能,他们在本地计算机和我们的开发服务器上工作得很好.但客户的数据库管理员不接受Laravel迁移.他为每个新版本的应用程序请求原始SQL脚本.
是否有任何工具或编程技术可以将Laravel迁移的输出捕获到上/下SQL脚本?
如果我们可以在创建生产构建时将SQL脚本生成集成到CI系统(TeamCity)中,那将是完美的.
顺便说一句,我们将在这个项目中使用Laravel 5和PostgreSQL.
我试图在Laravel 5中创建mysql表.我创建了一个/project/database/migrations名为的文件users.php:
[...]
public function up()
{
Schema::create('users', function(Blueprint $table)
{
$table->increments('id');
$table->string('username');
$table->string('fullname');
$table->int('number');
$table->string('email')->unique();
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();
});
}
[...]
Run Code Online (Sandbox Code Playgroud)
然后我尝试在project-folder中运行这些命令:
$ php artisan migrate
$ php artisan migrate:install
$ php artisan migrate --pretend
Run Code Online (Sandbox Code Playgroud)
它们都没有返回任何输出,也没有创建表.要填充的数据库存在.
我关注多对多Laravel关系中的自动命名表.
例如:
Schema::create('feature_product', function (Blueprint $table) {
Run Code Online (Sandbox Code Playgroud)
将表名更改为:
Schema::create('product_feature', function (Blueprint $table) {
Run Code Online (Sandbox Code Playgroud)
我的关系有误.
有什么问题product_feature?
跑步的时候我一直这样 php artisan migrate
SQLSTATE [42000]:语法错误或访问冲突:1091无法DROP'电子邮件'; 检查列/键是否存在
虽然我看到我的数据库中存在电子邮件.
我的迁移脚本.我试图放弃唯一约束.
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AlterGuestsTable3 extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('guests', function(Blueprint $table)
{
$table->dropUnique('email');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('guests', function(Blueprint $table)
{
$table->dropUnique('email');
});
}
}
Run Code Online (Sandbox Code Playgroud)
我忘了清除任何缓存吗?
有什么提示吗?
我需要将迁移列类型更改$table->string('text');为文本类型,我尝试以几种方式执行此操作,但它们都不起作用.是否可以在一次迁移中完成.我可以猜测删除列然后再使用新类型创建它,但我想知道是否可以在一次迁移中执行此操作?