ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 1. 기본 스키마 세팅
    Laravel/Chat 2021. 1. 15. 17:53
    반응형
    SMALL

    # What?

    - 채팅 구현을 위한 테이블 설계

     

    # How?

    php artisan make:model -m Message
    php artisan make:model -m Chat
    
    php artisan make:resource UserResource
    php artisan make:resource MessageResource
    php artisan make:resource ChatResource
    
    php artisan make:resource UserCollection
    php artisan make:resource MessageCollection
    php artisan make:resource ChatCollection
    
    php artisan make:controller UserController
    php artisan make:controller MessageController
    php artisan make:controller ChatController

     

    @ create_messages_table.php

            Schema::create('messages', function (Blueprint $table) {
                $table->id();
                $table->unsignedBigInteger("chat_id");
                $table->foreign("chat_id")->references("id")->on("chats")->onDelete("cascade");
                $table->unsignedBigInteger("sender_id");
                $table->foreign("sender_id")->references("id")->on("users")->onDelete("cascade");
                $table->unsignedBigInteger("receiver_id");
                $table->foreign("receiver_id")->references("id")->on("users")->onDelete("cascade");
                $table->index(["chat_id", "receiver_id"]);
                $table->boolean("read")->default(false);
                $table->string("body");
                $table->timestamps();
            });

    @ Message.php

    <?php
    
    namespace App\Models;
    
    use Illuminate\Database\Eloquent\Factories\HasFactory;
    use Illuminate\Database\Eloquent\Model;
    
    class Message extends Model
    {
        use HasFactory;
    
        protected $fillable = ["body", "chat_id", "receiver_id", "sender_id"];
    
        public function chat()
        {
            return $this->belongsTo(Chat::class);
        }
    
        public function user()
        {
            return $this->belongsTo(User::class);
        }
    }
    

     

    @ create_chats.php

        public function up()
        {
            Schema::create('chats', function (Blueprint $table) {
                $table->id();
                $table->timestamps();
            });
        }

    @ Chat.php

    class Chat extends Model
    {
        use HasFactory;
    
        protected $fillable = ["message_id", "session_id", "user_id", "read"];
    
        public function messages()
        {
            return $this->hasMany(Message::class);
        }
    }
    

    @ User.php

       public function chats()
        {
            return $this->belongsToMany(Chat::class);
        }

    @ create_chat_user_table.php

            Schema::create('chat_user', function (Blueprint $table) {
                $table->unsignedBigInteger("chat_id");
                $table->foreign("chat_id")->references("id")->on("chats")->onDelete("cascade");
                $table->unsignedBigInteger("user_id");
                $table->foreign("user_id")->references("id")->on("users")->onDelete("cascade");
                $table->index(["chat_id", "user_id"]);
                $table->unique(["chat_id", "user_id"]);
                $table->timestamps();
            });

     

    * 나머지 Resource, Collection은 알아서 설정하자.

     

    LIST

    'Laravel > Chat' 카테고리의 다른 글

    3. View단 세팅  (0) 2021.01.17
    2. Pusher 세팅(채팅)  (0) 2021.01.16

    댓글

Designed by Tistory.