Laravel 5.5 - 如何根据`id`值创建`negative_id`列值?

时间:2018-04-26 19:06:10

标签: laravel laravel-5.5 laravel-migrations

我正在进行测试,但不确定如何使用laravel migration file执行此操作。简而言之,有一个自动递增id字段(始终为正整数)。但我还想要一个negative_id字段,该字段是id * -1的值,并且在将id插入数据库时​​会自动插入同步。因此,当添加第一个用户时,id会自动添加为1,因此negative_id应添加-1作为该行的1 * -1 = -1,因为| id | negative_id ------------------ | 1 | -1 | 2 | -2 | 3 | -3 。该表应如下所示:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration {
    public function up(){
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('negative_id'); // how to make this id*-1 value
        });
    }

    public function down(){
        Schema::dropIfExists('users');
    }

以下是我当前的迁移文件:

Problems occurred when invoking code from plug-in: "org.eclipse.jface".
java.lang.NoClassDefFoundError: com/genuitec/eclipse/core/vU
    at   com.genuitec.eclipse.modernweb.angular2.ui.project.NewAngular2ProjectWizard.init(NewAngular2ProjectWizard.java:68)
    at org.eclipse.ui.internal.dialogs.WorkbenchWizardNode.getWizard(WorkbenchWizardNode.java:171)
at org.eclipse.jface.wizard.WizardSelectionPage.getNextPage(WizardSelectionPage.java:107)
at org.eclipse.ui.internal.dialogs.WorkbenchWizardSelectionPage.getNextPage(WorkbenchWizardSelectionPage.java:97)
at org.eclipse.ui.internal.dialogs.NewWizardSelectionPage.advanceToNextPageOrFinish(NewWizardSelectionPage.java:71)
at org.eclipse.ui.internal.dialogs.NewWizardNewPage.lambda$0(NewWizardNewPage.java:348)
at org.eclipse.jface.viewers.StructuredViewer$1.run(StructuredViewer.java:831)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
at org.eclipse.ui.internal.JFaceUtil.lambda$0(JFaceUtil.java:44)
at org.eclipse.jface.util.SafeRunnable.run(SafeRunnable.java:173)
at org.eclipse.jface.viewers.StructuredViewer.fireDoubleClick(StructuredViewer.java:828)
at org.eclipse.jface.viewers.AbstractTreeViewer.handleDoubleSelect(AbstractTreeViewer.java:1459)
at org.eclipse.jface.viewers.StructuredViewer$4.widgetDefaultSelected(StructuredViewer.java:1258)
at org.eclipse.jface.util.OpenStrategy.fireDefaultSelectionEvent(OpenStrategy.java:249)
at org.eclipse.jface.util.OpenStrategy.access$0(OpenStrategy.java:247)
at org.eclipse.jface.util.OpenStrategy$1.handleEvent(OpenStrategy.java:306)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:86)
at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4428)

java.lang.NoClassDefFoundError: com/genuitec/eclipse/core/vU
at com.genuitec.eclipse.modernweb.angular2.ui.project.NewAngular2ProjectWizard.init(NewAngular2ProjectWizard.java:68)
at org.eclipse.ui.internal.dialogs.WorkbenchWizardNode.getWizard(WorkbenchWizardNode.java:171)
at org.eclipse.jface.wizard.WizardSelectionPage.getNextPage(WizardSelectionPage.java:107)
at org.eclipse.ui.internal.dialogs.WorkbenchWizardSelectionPage.getNextPage(WorkbenchWizardSelectionPage.java:97)
at org.eclipse.jface.wizard.WizardDialog.nextPressed(WizardDialog.java:857)
at org.eclipse.jface.wizard.WizardDialog.buttonPressed(WizardDialog.java:413)
at org.eclipse.jface.dialogs.Dialog.lambda$0(Dialog.java:619)
at org.eclipse.swt.events.SelectionListener$1.widgetSelected(SelectionListener.java:81)
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:249)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:86)
at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4428)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1079)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4238)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3817)
at org.eclipse.jface.window.Window.runEventLoop(Window.java:818)
at org.eclipse.jface.window.Window.open(Window.java:794)
at org.eclipse.ui.actions.NewProjectAction.run(NewProjectAction.java:115)
at org.eclipse.jface.action.Action.runWithEvent(Action.java:473)
at org.eclipse.jface.action.ActionContributionItem.handleWidgetSelection(ActionContributionItem.java:565)
at org.eclipse.jface.action.ActionContributionItem.lambda$4(ActionContributionItem.java:397)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:86)
at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4428)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1079)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4238)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3817)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$5.run(PartRenderingEngine.java:1150)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:336)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1039)
at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:153)
at org.eclipse.ui.internal.Workbench.lambda$3(Workbench.java:680)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:336)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:594)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:148)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:151)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:134)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:104)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:388)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:243)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:653)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:590)
at org.eclipse.equinox.launcher.Main.run(Main.java:1499)

com.genuitec.eclipse.core.GenuitecCoreRTException: Invalid thread state detected on save.
at com.genuitec.eclipse.core.F.C(Unknown Source)
at com.genuitec.eclipse.core.F$1.run(Unknown Source)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:56)

知道如何从迁移文件中完成此操作吗?

1 个答案:

答案 0 :(得分:0)

您可能需要查看生成列

$table->integer('negative_id')->virtualAs('id*-1');

查看this链接了解详情。