app/Models/Item.php: change recipe relationship from belongsTo to hasMany.
This commit is contained in:
parent
7df1e8345d
commit
d3eccc596f
2 changed files with 11 additions and 5 deletions
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue