Skip to content

Commit e2fa80f

Browse files
Fixing posts->categories relationships.
1 parent 3fbdd8f commit e2fa80f

6 files changed

+16
-12
lines changed

app/PostCategory.php renamed to app/CategoriesPosts.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Illuminate\Database\Eloquent\Model;
66

7-
class PostCategory extends Model
7+
class CategoriesPosts extends Model
88
{
99
public function post() {
1010
return $this->belongsTo('App\Post', 'id');

app/Post.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Post extends Model
1313
// posts can have one or many categories
1414
// returns all categories associated to the post
1515
public function categories() {
16-
return $this->hasManyThrough('App\Category', 'App\Post');
16+
return $this->hasManyThrough('App\Category', 'App\Post', 'id', 'id');
1717
}
1818

1919
public function user() {

database/factories/PostCategoryFactory.php renamed to database/factories/CategoriesPostsFactory.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Faker\Generator as Faker;
66

7-
$factory->define(App\PostCategory::class, function (Faker $faker) {
7+
$factory->define(App\CategoriesPosts::class, function (Faker $faker) {
88
return [
99
'post_id' => App\Post::inRandomOrder()->first()->id,
1010
'category_id' => App\Category::inRandomOrder()->first()->id

database/migrations/2020_05_31_143819_create_table_post_category.php renamed to database/migrations/2020_05_31_143819_create_table_categories_posts.php

+10-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use Illuminate\Database\Schema\Blueprint;
55
use Illuminate\Support\Facades\Schema;
66

7-
class CreateTablePostCategory extends Migration
7+
class CreateTableCategoriesPosts extends Migration
88
{
99
/**
1010
* Run the migrations.
@@ -13,18 +13,22 @@ class CreateTablePostCategory extends Migration
1313
*/
1414
public function up()
1515
{
16-
Schema::create('post_category', function (Blueprint $table) {
17-
$table->id();
16+
Schema::create('categories_posts', function (Blueprint $table) {
17+
$table->unsignedBigInteger('category_id');
1818
$table->unsignedBigInteger('post_id');
19+
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
20+
$table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
21+
22+
23+
/*$table->unsignedBigInteger('post_id');
1924
$table->unsignedBigInteger('category_id');
2025
$table->unique(['post_id', 'category_id']);
2126
$table->foreign('post_id')
2227
->references('id')->on('posts')
2328
->onDelete('cascade');
2429
$table->foreign('category_id')
2530
->references('id')->on('categories')
26-
->onDelete('cascade');
27-
$table->timestamps();
31+
->onDelete('cascade');*/
2832
});
2933
}
3034

@@ -35,6 +39,6 @@ public function up()
3539
*/
3640
public function down()
3741
{
38-
Schema::dropIfExists('post_category');
42+
Schema::dropIfExists('categories_posts');
3943
}
4044
}

database/seeds/PostCategorySeeder.php renamed to database/seeds/CategoriesPostsSeeder.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use Illuminate\Database\Seeder;
44

5-
class PostCategorySeeder extends Seeder
5+
class CategoriesPostsSeeder extends Seeder
66
{
77
/**
88
* Run the database seeds.
@@ -11,6 +11,6 @@ class PostCategorySeeder extends Seeder
1111
*/
1212
public function run()
1313
{
14-
factory(App\PostCategory::class, 10)->create();
14+
factory(App\CategoriesPosts::class, 10)->create();
1515
}
1616
}

database/seeds/DatabaseSeeder.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function run()
1515
UserSeeder::class,
1616
PostSeeder::class,
1717
CategorySeeder::class,
18-
PostCategorySeeder::class
18+
CategoriesPostsSeeder::class
1919
]);
2020
}
2121
}

0 commit comments

Comments
 (0)