app/Models/Item.php: add reagent_for relationship.
This commit is contained in:
parent
f9d44de6cb
commit
6f7c65fab3
2 changed files with 30 additions and 0 deletions
|
|
@ -47,6 +47,15 @@ class Item extends Model
|
|||
return $this->hasMany(Recipe::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get recipes that this item is reagent for.
|
||||
*/
|
||||
public function reagent_for()
|
||||
{
|
||||
return $this->belongsToMany(Recipe::class, 'reagents')
|
||||
->withPivot('quantity');
|
||||
}
|
||||
|
||||
public function getQuantityAttribute()
|
||||
{
|
||||
if ($this->pivot) {
|
||||
|
|
|
|||
|
|
@ -33,6 +33,27 @@ class ItemTest extends TestCase
|
|||
$this->assertEquals($recipe2->id, $item->recipes[1]->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test reagent for relationship.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_reagent_for_relationship()
|
||||
{
|
||||
$item = Item::factory()->create();
|
||||
|
||||
$recipe = Recipe::factory()
|
||||
->hasAttached($item, ['quantity' => 1], 'reagents')
|
||||
->create();
|
||||
|
||||
$recipe2 = Recipe::factory()
|
||||
->hasAttached($item, ['quantity' => 1], 'reagents')
|
||||
->create();
|
||||
|
||||
$this->assertEquals($recipe->id, $item->reagent_for[0]->id);
|
||||
$this->assertEquals($recipe2->id, $item->reagent_for[1]->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test quantity attribute
|
||||
*
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue