Add Recipe model

This commit is contained in:
Christopher C. Wells 2020-12-21 15:25:10 -08:00
parent a4cc3c3f65
commit d54ad13460
10 changed files with 305 additions and 45 deletions

View File

@ -0,0 +1,85 @@
<?php
namespace App\Http\Controllers;
use App\Models\Recipe;
use Illuminate\Http\Request;
class RecipeController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param \App\Models\Recipe $recipe
* @return \Illuminate\Http\Response
*/
public function show(Recipe $recipe)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Models\Recipe $recipe
* @return \Illuminate\Http\Response
*/
public function edit(Recipe $recipe)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Recipe $recipe
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Recipe $recipe)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\Recipe $recipe
* @return \Illuminate\Http\Response
*/
public function destroy(Recipe $recipe)
{
//
}
}

View File

@ -4,7 +4,7 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class IngredientAmount extends Model
{
@ -15,12 +15,20 @@ class IngredientAmount extends Model
*/
protected array $fillable = [
'amount',
'weight',
];
/**
* Get the Ingredient this amount pertains to.
* Get the Ingredient this amount belongs to.
*/
public function ingredient(): HasOne {
return $this->hasOne(Ingredient::class);
public function ingredient(): BelongsTo {
return $this->belongsTo(Ingredient::class);
}
/**
* Get the Recipe this amount belongs to.
*/
public function recipe(): BelongsTo {
return $this->belongsTo(Recipe::class);
}
}

27
app/Models/Recipe.php Normal file
View File

@ -0,0 +1,27 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
class Recipe extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*/
protected array $fillable = [
'name',
];
/**
* Get the Ingredient Amounts used for this Recipe.
*/
public function ingredientAmounts(): HasMany {
return $this->hasMany(IngredientAmount::class);
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace Database\Factories;
use App\Models\Recipe;
use Illuminate\Database\Eloquent\Factories\Factory;
class RecipeFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Recipe::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
//
];
}
}

View File

@ -1,6 +1,7 @@
<?php
use App\Models\Ingredient;
use App\Models\Recipe;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
@ -16,8 +17,10 @@ class CreateIngredientAmountsTable extends Migration
{
Schema::create('ingredient_amounts', function (Blueprint $table) {
$table->id();
$table->unsignedFloat('amount');
$table->foreignIdFor(Ingredient::class);
$table->unsignedFloat('amount');
$table->foreignIdFor(Recipe::class);
$table->unsignedInteger('weight');
$table->timestamps();
});
}

View File

@ -0,0 +1,32 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateRecipesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('recipes', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('recipes');
}
}

View File

@ -8,11 +8,10 @@ class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
public function run(): void
{
$this->call(IngredientSeeder::class);
$this->call(RecipeSeeder::class);
}
}

View File

@ -1,18 +0,0 @@
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
class IngredientAmountSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
//
}
}

View File

@ -9,26 +9,32 @@ class IngredientSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
public function run(): void
{
$default_ingredients = [
[
'name' => 'black beans',
'unit' => 'cup',
'calories' => 236,
'protein' => 15.9,
'fat' => 0.972,
'carbohydrates' => 42.3
'name' => 'baking powder',
'unit' => 'tsp',
'calories' => 2.44,
'protein' => 0,
'fat' => 0,
'carbohydrates' => 1.27
],
[
'name' => 'egg white',
'calories' => 17.2,
'protein' => 3.6,
'fat' => 0.056,
'carbohydrates' => 0.241
'name' => 'egg',
'calories' => 71.5,
'protein' => 6.28,
'fat' => 4.76,
'carbohydrates' => 0.36
],
[
'name' => 'flour, all-purpose',
'unit' => 'cup',
'calories' => 455,
'protein' => 12.9,
'fat' => 1.22,
'carbohydrates' => 95.4
],
[
'name' => 'milk, whole',
@ -39,12 +45,28 @@ class IngredientSeeder extends Seeder
'carbohydrates' => 11.4
],
[
'name' => 'rice, brown (cooked)',
'name' => 'salt',
'unit' => 'tsp',
'calories' => 0,
'protein' => 0,
'fat' => 0,
'carbohydrates' => 0
],
[
'name' => 'sugar, white',
'unit' => 'cup',
'calories' => 238,
'protein' => 5.32,
'fat' => 1.87,
'carbohydrates' => 49.6
'calories' => 770,
'protein' => 0,
'fat' => 0.54,
'carbohydrates' => 199
],
[
'name' => 'vegetable oil',
'unit' => 'tbsp',
'calories' => 124,
'protein' => 0,
'fat' => 14,
'carbohydrates' => 199
],
];
Ingredient::factory()->createMany($default_ingredients);

View File

@ -0,0 +1,74 @@
<?php
namespace Database\Seeders;
use App\Models\Ingredient;
use App\Models\IngredientAmount;
use App\Models\Recipe;
use Illuminate\Database\Seeder;
class RecipeSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
/** @var \App\Models\Recipe $recipe */
$recipe = Recipe::factory()->create(['name' => 'pancakes']);
$weight = 0;
$amounts = [
[
'ingredient_id' => Ingredient::where('name', 'flour, all-purpose')
->first()->id,
'amount' => 1,
'recipe_id' => $recipe->id,
'weight' => $weight++,
],
[
'ingredient_id' => Ingredient::where('name', 'sugar, white')
->first()->id,
'amount' => 0.125,
'recipe_id' => $recipe->id,
'weight' => $weight++,
],
[
'ingredient_id' => Ingredient::where('name', 'baking powder')
->first()->id,
'amount' => 2,
'recipe_id' => $recipe->id,
'weight' => $weight++,
],
[
'ingredient_id' => Ingredient::where('name', 'salt')
->first()->id,
'amount' => 1,
'recipe_id' => $recipe->id,
'weight' => $weight++,
],
[
'ingredient_id' => Ingredient::where('name', 'egg')
->first()->id,
'amount' => 1,
'recipe_id' => $recipe->id,
'weight' => $weight++,
],
[
'ingredient_id' => Ingredient::where('name', 'milk, whole')
->first()->id,
'amount' => 1,
'recipe_id' => $recipe->id,
'weight' => $weight++,
],
[
'ingredient_id' => Ingredient::where('name', 'vegetable oil')
->first()->id,
'amount' => 2,
'recipe_id' => $recipe->id,
'weight' => $weight++,
],
];
IngredientAmount::factory()->createMany($amounts);
}
}