0

This is my structure, I want to connect these two tables in laravel, How to do it?

Post Table:

public function up()
{
    Schema::create('post', function (Blueprint $table) {
        $table->increments('post_id');
        $table->string('post');
        $table->integer('time');
        $table->string('host');
        $table->integer('vote_up');
        $table->integer('vote_down');
        $table->foreign('id_fk')->references('id')->on('users');
        $table->timestamps();
    });
}

Users Table:

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->date('dob');
        $table->string('email')->unique();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
}
Chris Burton
  • 1,195
  • 6
  • 19
  • 54
Mochi
  • 123
  • 8
  • 1
    What do you mean by "connect these two tables"? Do you just want to create a relationship between the models? – jackel414 May 15 '17 at 16:47

2 Answers2

2

I presume you are just pasting in migrations but you do need to have yours users table created before your posts table. I would change

$table->foreign('id_fk')->references('id')->on('users');

to

$table->foreign('user_id')->references('id')->on('users');

Because Laravel can infer foreign keys:

Eloquent determines the default foreign key name by examining the name of the relationship method and suffixing the method name with _id. However, if the foreign key on the Post model is not user_id, you may pass a custom key name as the second argument to the belongsTo method

And then all you need in your models is the following:

class Post extends Model
{
    /**
     * Get the user that owns the post.
     */
    public function user()
    {
        return $this->belongsTo('App\User');
        // if you want to keep your current structure:
        // return $this->belongsTo('App\User', 'id_fk', 'id);
    }
}

and

class User extends Model
{
    /**
     * Get the post for a user.
     */
    public function posts()
    {
        return $this->hasMany('App\Post');
        // if you want to keep your current structure:
        // return $this->belongsTo('App\Post', 'id_fk');
    }
}

You can read more about setting up relationships here.

Alex Harris
  • 6,172
  • 2
  • 32
  • 57
  • Thank you so much but i deleted my database and recreated it by using other name and now it shows "[Illuminate\Database\QueryException] SQLSTATE[HY000] [1049] Unknown database 'chatty' (SQL: select * from information_schema.tables where table_schema = chatty and table_name = migrations) [PDOException] SQLSTATE[HY000] [1049] Unknown database 'chatty' " (chatty is my previous databas) – Mochi May 25 '17 at 14:02
  • This would probably be best as a separate question but did you change your `config/database.php`? – Alex Harris May 25 '17 at 14:34
  • No that one is my mistake sorry i found the mistake now but back to my previous question I tried like you commented, but it doesnt work – Mochi May 25 '17 at 14:49
  • class Post extends Migration{ use Illuminate\Database\Eloquent\Model; public function up(){ Schema::create('post', function (Blueprint $table) { $table->increments('post_id');$table->string('post');$table->integer('time');$table->string('host');$table->integer('vote_up');$table->integer('vote_down');$table->foreign('user_id')->references('id')->on('users');$table->timestamps();});} public function down(){ Schema::dropIfExists('post');} public function user(){ return $this->belongsTo('App\User');}} – Mochi May 25 '17 at 14:53
  • please update your question with the code, it is extremely hard to read like this. Also please add what error or issue you are getting. – Alex Harris May 25 '17 at 15:06
  • Alex Harris Can i get your facebook name if you don't mind? – Mochi May 27 '17 at 14:50
0

In your posts table you should have:

$table->integer('user_id')->unsigned();

on your User model:

public function posts(){
return $this->hasMany(Post::class);
}

on your Post model:

public function user(){
return $this->belongsTo(User::class);
}
ambrooo
  • 134
  • 5
  • Or you could keep your table structure and define the foreign key on the relationship: `public function posts(){ return $this->hasMany(Post::class, 'id_fk'); }` `public function user(){ return $this->belongsTo(User::class, 'id_fk'); }` – ambrooo May 15 '17 at 17:03
  • Thank you so much but i deleted my database and recreated it by using other name and now it shows "[Illuminate\Database\QueryException] SQLSTATE[HY000] [1049] Unknown database 'chatty' (SQL: select * from information_schema.tables where table_schema = chatty and table_name = migrations) [PDOException] SQLSTATE[HY000] [1049] Unknown database 'chatty' " (chatty is my previous databas) – Mochi May 25 '17 at 14:01
  • In console run composer dump-autoload – ambrooo May 25 '17 at 14:03
  • [Illuminate\Database\QueryException] SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists (SQL: create table `users` (` user_id` int unsigned not null, `name` varchar(255) not null, `dob` date not null, `email` varchar(255) not null, ` password` varchar(255) not null, `remember_token` varchar(100) null, `created_at` timestamp null, `updated_at` time stamp null) default character set utf8mb4 collate utf8mb4_unicode_ci) [PDOException] SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists – Mochi May 25 '17 at 14:58
  • After following what you have said this is what i got – Mochi May 25 '17 at 14:58