How to get data from a table which does not have relationship with the other one.

Posted 1 year ago by agilasadi

Ok so, I have a comments table where I store user_id and it is related with the user table, I can get anything from there but than there is userprofile table which has nothing to do with user table, but I want to user user_id from comments table to access userprofile table id, I don't know how to define the foreginkey and stuff in the relationship. my comments table looks like:

      $table->increments('id');
      $table->string('user_id');
      $table->string('comment');
      $table->integer('article_id');
      $table->timestamps();

and userprofil looks like:

      $table->increments('id');
      $table->string('username');
      $table->string('role')->default('okuyucu');
      $table->string('email')->unique();
      $table->string('userImg')->default('profile.jpg');
      $table->string('nameSurname')->default('Not Added');
      $table->string('bio')->default('Reader');
      $table->integer('reputation')->default('0');
      $table->integer('followers')->default('0');
      $table->timestamps();

and I'm guessing I need a relationship in comments which look like something:

public function userprofil()
{
    return $this->hasOne('App\Userprofile');
}

but somehow I need to use user_id from comments as id in userprofile table.

Sry fro messy look, it is my first time I'm asking a question in laracasts

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

Reply to

Use Markdown with GitHub-flavored code blocks.