Php 如何在laravel 5.6中混合时检查mongo收集是否存在?
我正在我的一个项目中使用Laravel5.6、mongodb和mysql。我使用jessengers mongodb包,并通过它为我的3个集合创建了模式,尽管mongodb是无模式数据库,但出于文档目的,我创建了模式。其中一个例子是:Php 如何在laravel 5.6中混合时检查mongo收集是否存在?,php,mongodb,laravel,Php,Mongodb,Laravel,我正在我的一个项目中使用Laravel5.6、mongodb和mysql。我使用jessengers mongodb包,并通过它为我的3个集合创建了模式,尽管mongodb是无模式数据库,但出于文档目的,我创建了模式。其中一个例子是: <?php use Jenssegers\Mongodb\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateChatMessagesTable exte
<?php
use Jenssegers\Mongodb\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateChatMessagesTable extends Migration
{
/**
* The name of the database connection to use.
*
* @var string
*/
protected $connection = 'mongodb';
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::connection($this->connection)->create('chat_messages', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->integer('trade_id');
$table->tinyInteger('type')->default('1');
$table->text('content');
$table->integer('recipient_id');
$table->timestamp('recipient_read_at')->nullable();
$table->timestamp('created_at')->nullable();
$table->tinyInteger('status')->default('1');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::connection($this->connection)
->table('chat_messages', function (Blueprint $collection)
{
$collection->drop();
});
}
}
仅在迁移文件中,但我从未尝试过这种方法,我将其保留为最后一个解决方案。请指导和帮助我。我在搜索了拉威尔的文档后得到了解决方案 有一个方法叫做:
hasTable()
,它返回boolean
值来判断集合/表是否存在
我就是这样做的,现在它工作得很好:
<?php
use Jenssegers\Mongodb\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateChatMessagesTable extends Migration
{
/**
* The name of the database connection to use.
*
* @var string
*/
protected $connection = 'mongodb';
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
if(Schema::connection($this->connection)->hasTable('chat_messages') == false) {
Schema::connection($this->connection)->create('chat_messages', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->integer('trade_id');
$table->tinyInteger('type')->default('1');
$table->text('content');
$table->integer('recipient_id');
$table->timestamp('recipient_read_at')->nullable();
$table->timestamp('created_at')->nullable();
$table->tinyInteger('status')->default('1');
});
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
if(Schema::connection($this->connection)->hasTable('chat_messages') == false) {
Schema::connection($this->connection)
->table('chat_messages', function (Blueprint $collection) {
$collection->drop();
});
}
}
}
<?php
use Jenssegers\Mongodb\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateChatMessagesTable extends Migration
{
/**
* The name of the database connection to use.
*
* @var string
*/
protected $connection = 'mongodb';
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
if(Schema::connection($this->connection)->hasTable('chat_messages') == false) {
Schema::connection($this->connection)->create('chat_messages', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->integer('trade_id');
$table->tinyInteger('type')->default('1');
$table->text('content');
$table->integer('recipient_id');
$table->timestamp('recipient_read_at')->nullable();
$table->timestamp('created_at')->nullable();
$table->tinyInteger('status')->default('1');
});
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
if(Schema::connection($this->connection)->hasTable('chat_messages') == false) {
Schema::connection($this->connection)
->table('chat_messages', function (Blueprint $collection) {
$collection->drop();
});
}
}
}