2 months ago

DB Question

Posted 2 months ago by artisticre

I have two tables Users and Posts. I am trying to understand relationships. I have already created these tables and everything is working. Now I want to create a controller that lists the posts based on the user_id. Would I put a column in the Posts table called user_id to be able to query the db or is there a better way?

User Model

 public function posts() {
        return $this->belongsToMany('App\Post');

Post Model

public function users(){
        return $this->belongsToMany('App\User');

Users Table

 public function up()
        Schema::create('users', function (Blueprint $table) {

Posts Table

public function up()
        Schema::create('posts', function (Blueprint $table) {

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