如何根据laravel 5.6中的id检查唯一值?

时间:2018-09-18 11:11:01

标签: php laravel-5

我有一个表格,即子菜单

 Schema::create('submenu', function (Blueprint $table) {
        $table->increments('submenu_id');
        $table->tinyInteger('status')->default(1);
        $table->unsignedInteger('menu_id');
        $table->string('name');
       $table->tinyInteger('order');
        $table->timestamps();

        $table->foreign('menu_id')
            ->references('id')
            ->on('menu')
            ->onDelete('cascade');
    });

在此表中,我们在文本框中输入顺序的值,即1,2,3,4。 在此,根据menu_id,顺序必须从1开始。像menu_id = 1且order = 1且order = 2一样,但是问题在于订单必须是唯一的。如果menu_id = 1中已经有order = 1,那么它必须显示错误消息“ order has exist”。

现在,下一个menu_id = 2也可以具有order = 1和order = 2。 那么,如何根据menu_id保持唯一值并检查menu_id是否包含顺序?

SubmenuController

   $order = Input::get('order');
        $menu = Input::get('menu_id');
        if($menu->contains($order)){
            echo "exists";die;
        } else {
            echo "no exists";die;
        }

我已经在控制器中使用了它,但是它不起作用...

2 个答案:

答案 0 :(得分:0)

创建两列的复合索引:

Schema::create('submenu', function (Blueprint $table) {
    [...]
    $table->unique(['menu_id', 'order']);
});

在控制器中,您可以像这样处理重复的组合:

try {
    Submenu::create($data);
} catch(Exception $exception) {
    // This combination of menu_id and order already exists.
}

答案 1 :(得分:0)

您可以在执行某些操作之前在控制器中运行Select查询

from keras.layers import Dense, Embedding, Dropout, LSTM
from keras.models import Sequential
from keras.layers import Bidirectional
from keras.layers.convolutional import Conv1D
from keras.layers.convolutional import MaxPooling1D

embed_dim = 100
lstm_out = 128
max_features = 5000

model8 = Sequential()
model8.add(Embedding(max_features, embed_dim, input_length = X.shape[1]))
model8.add(Dropout(0.2))
model8.add(Conv1D(filters=100, kernel_size=3, padding='same',  activation='relu'))
model8.add(MaxPooling1D(pool_size=2))
model8.add(Bidirectional(LSTM(lstm_out)))
model8.add(Dense(124,activation='softmax'))
model8.compile(loss = 'categorical_crossentropy', optimizer='adam',metrics = 
['accuracy'])
print(model8.summary())