If you haven't read the last post you can find it here
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title');
$table->mediumText('ingredients');
$table->mediumText('body');
$table->mediumText('step1');
$table->mediumText('step2')->nullable();
$table->mediumText('step3')->nullable();
$table->mediumText('step4')->nullable();
$table->mediumText('step5')->nullable();
$table->mediumText('step6')->nullable();
$table->mediumText('step7')->nullable();
$table->mediumText('step8')->nullable();
$table->mediumText('step9')->nullable();
$table->mediumText('step10')->nullable();
$table->mediumText('step11')->nullable();
$table->mediumText('step12')->nullable();
$table->mediumText('step13')->nullable();
$table->mediumText('step14')->nullable();
$table->mediumText('step15')->nullable();
$table->timestamps();
//$table->enum('level', ['easy', 'hard']);
});
}
This is my approach:
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title');
$table->mediumText('ingredients');
$table->mediumText('body');
$table->mediumText('step1');
for ($x = 2; $x < 16; $x++) {
$guff = 'step'.$x;
$table->mediumText($guff)->nullable();
}
$table->timestamps();
//$table->enum('level', ['easy', 'hard']);
});
}
Now I need to run a terminal command to do a fresh DB migration
php artisan migrate:fresh
And there we have some cleaned up DRY code. It may not seem like big progress but cleaning up the code will firstly make it run a little better but also allows for future changes to be made easier.
For example, I have set a limit to 15 steps for a recipe, I originally chose this as I didn't want to code in any more than 15 as it was looking messy. Now I've created the code in for loops extending the limit of steps is now a much simpler task. And requires changing the number in the for loops... :)