Skip to content

Instantly share code, notes, and snippets.

@densityx
Last active September 11, 2015 01:36
Show Gist options
  • Save densityx/003b3bf6c798706e329a to your computer and use it in GitHub Desktop.
Save densityx/003b3bf6c798706e329a to your computer and use it in GitHub Desktop.
#User Model
===========
<?php
... class User
{
public function videos() {
return $this->belongsTo('Watch\Video');
}
public function episodes() {
return $this->hasMany('Watch\Episode');
}
}
#Video Model
============
<?php
... class Video
{
public function users() {
return $this->hasMany('Watch\User');
}
public function episodes() {
return $this->hasManyThrough('Watch\Episode', 'Watch\User');
}
}
#Episode Model
============
<?php
... class Episode
{
public function user() {
return $this->belongsTo('Watch\User');
}
}
#Migration
==========
#User
=====
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->integer('episode_id')->unsigned();
$table->string('name');
$table->string('email')->unique();
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();
$table->foreign('episode_id')
->references('id')
->on('episodes')
->onDelete('cascade');
});
#Video
======
Schema::create('videos', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->text('description');
$table->timestamps();
});
#Episode
========
Schema::create('episodes', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->string('name');
$table->timestamps();
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
});
Ketika run 'php artisan migrate' kok dapat error ya?
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `users` add const
raint users_episode_id_foreign foreign key (`episode_id`) references `episodes` (`id`) on delete cascade)
[PDOException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
:( sudah 2 hari ngak dapat-dapat... tolong dong
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment