2022_04_20_061915_create_conversations_table.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. use Illuminate\Database\Migrations\Migration;
  3. use Illuminate\Database\Schema\Blueprint;
  4. use Illuminate\Support\Facades\Schema;
  5. use App\DirectMessage;
  6. use App\Models\Conversation;
  7. use Illuminate\Support\Facades\DB;
  8. class CreateConversationsTable extends Migration
  9. {
  10. /**
  11. * Run the migrations.
  12. *
  13. * @return void
  14. */
  15. public function up()
  16. {
  17. Schema::dropIfExists('conversations');
  18. Schema::create('conversations', function (Blueprint $table) {
  19. $table->bigIncrements('id');
  20. $table->bigInteger('to_id')->unsigned()->index();
  21. $table->bigInteger('from_id')->unsigned()->index();
  22. $table->bigInteger('dm_id')->unsigned()->nullable();
  23. $table->bigInteger('status_id')->unsigned()->nullable();
  24. $table->string('type')->nullable();
  25. $table->boolean('is_hidden')->default(false)->index();
  26. $table->boolean('has_seen')->default(false)->index();
  27. $table->json('metadata')->nullable();
  28. $table->unique(['to_id', 'from_id']);
  29. $table->timestamps();
  30. });
  31. sleep(10);
  32. if(DirectMessage::count()) {
  33. foreach(DirectMessage::lazy() as $msg) {
  34. Conversation::updateOrInsert([
  35. 'to_id' => $msg->to_id,
  36. 'from_id' => $msg->from_id,
  37. ],
  38. [
  39. 'dm_id' => $msg->id,
  40. 'status_id' => $msg->status_id,
  41. 'type' => $msg->type,
  42. 'created_at' => $msg->created_at,
  43. 'updated_at' => $msg->updated_at
  44. ]);
  45. }
  46. }
  47. }
  48. /**
  49. * Reverse the migrations.
  50. *
  51. * @return void
  52. */
  53. public function down()
  54. {
  55. Schema::dropIfExists('conversations');
  56. }
  57. }