From d3eccc596f460ff25c0a84331b2db022b24f42da Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Fri, 23 Jul 2021 13:33:10 +0200 Subject: [PATCH] app/Models/Item.php: change recipe relationship from belongsTo to hasMany. --- app/Models/Item.php | 4 ++-- tests/Feature/Models/ItemTest.php | 12 +++++++++--- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/app/Models/Item.php b/app/Models/Item.php index 8b56cc5..4008b55 100644 --- a/app/Models/Item.php +++ b/app/Models/Item.php @@ -42,9 +42,9 @@ class Item extends Model return 'slug'; } - public function recipe() + public function recipes() { - return $this->belongsTo(Recipe::class, 'id'); + return $this->hasMany(Recipe::class); } public function getQuantityAttribute() diff --git a/tests/Feature/Models/ItemTest.php b/tests/Feature/Models/ItemTest.php index a58f441..dccee93 100644 --- a/tests/Feature/Models/ItemTest.php +++ b/tests/Feature/Models/ItemTest.php @@ -13,18 +13,24 @@ class ItemTest extends TestCase use RefreshDatabase; /** - * Test recipe relationship. + * Test recipes relationship. * * @return void */ - public function test_recipe_relationship() + public function test_recipes_relationship() { $item = Item::factory()->create(); + $recipe = Recipe::factory() ->for($item, 'craft') ->create(); - $this->assertEquals($recipe->id, $item->recipe->id); + $recipe2 = Recipe::factory() + ->for($item, 'craft') + ->create(); + + $this->assertEquals($recipe->id, $item->recipes[0]->id); + $this->assertEquals($recipe2->id, $item->recipes[1]->id); } /**