nanadjei2
1 year ago
102
3
Laravel

Get the user who belongs to a thread.

Posted 1 year ago by nanadjei2

I have a threads table where either a user or admin can post a new thread.That means I have a "users table" and "admin table" . I want to retrieve the user's name of each thread. Can anyone help me out please.

This is my migrations;

  public function up()
       {
          Schema::create('threads', function (Blueprint $table) {
              $table->engine = 'InnoDB';
             $table->increments('id');
              // $table->unsignedInteger('user_id')->reference('id')->on('users')->onDelete('restrict');
        
             $table->unsignedInteger('user_id');
             $table->string('user_type', 50)->nullable();

             $table->unsignedInteger('category_id')->reference('id')->on('categories')->onDelete('restrict');        
             $table->string('title');
             $table->text('content');
             $table->bigInteger('likes')->nullable();
             $table->bigInteger('views')->nullable();
             $table->enum('resolved_status', ['Resolved', 'Awaiting...'])->default('Awaiting...');
             $table->enum('status', ['Draft', 'Published'])->default('Draft');
             $table->timestamps();
           });
       }

This is my relationship in Threads.php

              public function user()
            {
                 return $this->morphMany(User::class, 'users', 'user_type', 'user_id');
            }

This is the link to my db image https://pasteboard.co/HjsK6hd.jpg

Please sign in or create an account to participate in this conversation.