Add RecipeStep model

This commit is contained in:
Christopher C. Wells 2020-12-21 21:23:38 -08:00
parent 361d3b3a07
commit 2d1990d415
9 changed files with 220 additions and 3 deletions

View File

@ -0,0 +1,85 @@
<?php
namespace App\Http\Controllers;
use App\Models\RecipeStep;
use Illuminate\Http\Request;
class RecipeStepController 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\RecipeStep $recipeStep
* @return \Illuminate\Http\Response
*/
public function show(RecipeStep $recipeStep)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Models\RecipeStep $recipeStep
* @return \Illuminate\Http\Response
*/
public function edit(RecipeStep $recipeStep)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\RecipeStep $recipeStep
* @return \Illuminate\Http\Response
*/
public function update(Request $request, RecipeStep $recipeStep)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\RecipeStep $recipeStep
* @return \Illuminate\Http\Response
*/
public function destroy(RecipeStep $recipeStep)
{
//
}
}

View File

@ -6,6 +6,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
/**
* @property int id
* @property string name
* @property string unit
* @property float calories

View File

@ -7,6 +7,7 @@ use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
/**
* @property int id
* @property float amount
* @property float unit
* @property int weight

View File

@ -7,6 +7,10 @@ use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
/**
* @property int id
* @property string name
* @property string description
* @property int servings
* @property \App\Models\IngredientAmount[] ingredientAmounts
*/
class Recipe extends Model
@ -18,18 +22,27 @@ class Recipe extends Model
*/
protected array $fillable = [
'name',
'description',
'servings',
];
/**
* @inheritdoc
*/
protected array $with = ['ingredientAmounts'];
protected array $with = ['steps', 'ingredientAmounts'];
/**
* Get the steps for this Recipe.
*/
public function steps(): HasMany {
return $this->hasMany(RecipeStep::class)->orderBy('number');
}
/**
* Get the Ingredient Amounts used for this Recipe.
*/
public function ingredientAmounts(): HasMany {
return $this->hasMany(IngredientAmount::class);
return $this->hasMany(IngredientAmount::class)->orderBy('weight');
}
/**

33
app/Models/RecipeStep.php Normal file
View File

@ -0,0 +1,33 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
/**
* @property int id
* @property int number
* @property string step
* @property \App\Models\Recipe recipe
*/
class RecipeStep extends Model
{
use HasFactory;
/**
* @inheritdoc
*/
protected array $fillable = [
'number',
'step',
];
/**
* Get the Recipe this step belongs to.
*/
public function recipe(): BelongsTo {
return $this->belongsTo(Recipe::class);
}
}

View File

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

View File

@ -16,6 +16,8 @@ class CreateRecipesTable extends Migration
Schema::create('recipes', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->longText('description');
$table->unsignedInteger('servings');
$table->timestamps();
});
}

View File

@ -0,0 +1,35 @@
<?php
use App\Models\Recipe;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateRecipeStepsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('recipe_steps', function (Blueprint $table) {
$table->id();
$table->foreignIdFor(Recipe::class);
$table->unsignedInteger('number');
$table->longText('step');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('recipe_steps');
}
}

View File

@ -5,6 +5,7 @@ namespace Database\Seeders;
use App\Models\Ingredient;
use App\Models\IngredientAmount;
use App\Models\Recipe;
use App\Models\RecipeStep;
use Illuminate\Database\Seeder;
class RecipeSeeder extends Seeder
@ -15,7 +16,11 @@ class RecipeSeeder extends Seeder
public function run(): void
{
/** @var \App\Models\Recipe $recipe */
$recipe = Recipe::factory()->create(['name' => 'pancakes']);
$recipe = Recipe::factory()->create([
'name' => 'pancakes',
'description' => 'Basic pancakes in two steps.',
'servings' => 4,
]);
$weight = 0;
$amounts = [
@ -76,5 +81,19 @@ class RecipeSeeder extends Seeder
],
];
IngredientAmount::factory()->createMany($amounts);
$steps = [
[
'recipe_id' => $recipe->id,
'number' => 1,
'step' => 'In a large bowl, mix flour, sugar, baking powder and salt. Make a well in the center, and pour in milk, egg and oil. Mix until smooth.',
],
[
'recipe_id' => $recipe->id,
'number' => 2,
'step' => 'Heat a lightly oiled griddle or frying pan over medium high heat. Pour or scoop the batter onto the griddle, using approximately 1/4 cup for each pancake. Brown on both sides and serve hot.',
]
];
RecipeStep::factory()->createMany($steps);
}
}