From 7ee58d18fe63c835c507bff278cb81aa45022e89 Mon Sep 17 00:00:00 2001 From: Beth Parker Date: Fri, 11 Feb 2022 23:02:48 -0600 Subject: [PATCH] Added categories along w/ category autocomplete/filter --- app/Http/Controllers/RecipeController.php | 9 ++- app/Recipe.php | 5 ++ app/RecipeCategory.php | 17 +++++ ...12_000441_create_recipe_category_table.php | 35 +++++++++ database/seeders/RecipeSeeder.php | 13 ++++ resources/views/recipes/index.blade.php | 76 ++++++++++++------- 6 files changed, 124 insertions(+), 31 deletions(-) create mode 100644 app/RecipeCategory.php create mode 100644 database/migrations/2022_02_12_000441_create_recipe_category_table.php diff --git a/app/Http/Controllers/RecipeController.php b/app/Http/Controllers/RecipeController.php index d6c2eea..88ea1fe 100644 --- a/app/Http/Controllers/RecipeController.php +++ b/app/Http/Controllers/RecipeController.php @@ -5,19 +5,20 @@ namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Recipe; use App\RecipeIngredient; +use App\RecipeCategory; use App\User; class RecipeController extends Controller { public function index() { - //Pass all users - $recipes = Recipe::with('user')->get(); - return view('recipes.index')->with('recipes', $recipes); + $recipes = Recipe::with('user')->with('categories')->get(); + $categories=array_values(RecipeCategory::get()->sortby('name')->pluck('name')->unique()->toArray()); + return view('recipes.index')->with('recipes', $recipes)->with('categories',$categories); } public function show($id) { - $recipe=Recipe::with('user')->with('ingredients')->findOrFail($id); + $recipe=Recipe::with('user')->with('ingredients')->findOrFail($id); return view('recipes.show')->with('recipe', $recipe); } public function edit($id){ diff --git a/app/Recipe.php b/app/Recipe.php index 665fd6a..f3bc920 100644 --- a/app/Recipe.php +++ b/app/Recipe.php @@ -28,4 +28,9 @@ class Recipe extends Model { return $this->hasMany('App\RecipeIngredient'); } + + public function categories() + { + return $this->hasMany('App\RecipeCategory'); + } } diff --git a/app/RecipeCategory.php b/app/RecipeCategory.php new file mode 100644 index 0000000..b610535 --- /dev/null +++ b/app/RecipeCategory.php @@ -0,0 +1,17 @@ +belongsTo('App\Recipe'); + } +} diff --git a/database/migrations/2022_02_12_000441_create_recipe_category_table.php b/database/migrations/2022_02_12_000441_create_recipe_category_table.php new file mode 100644 index 0000000..857f1d8 --- /dev/null +++ b/database/migrations/2022_02_12_000441_create_recipe_category_table.php @@ -0,0 +1,35 @@ +id(); + $table->unsignedInteger('recipe_id'); + $table->string('name'); + $table->timestamps(); + + $table->foreign('recipe_id')->references('id')->on('recipes'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('recipe_category'); + } +} diff --git a/database/seeders/RecipeSeeder.php b/database/seeders/RecipeSeeder.php index f8eaf04..459f2a0 100644 --- a/database/seeders/RecipeSeeder.php +++ b/database/seeders/RecipeSeeder.php @@ -6,6 +6,7 @@ use Illuminate\Database\Seeder; use App\User; use App\Recipe; use App\RecipeIngredient; +use App\RecipeCategory; class RecipeSeeder extends Seeder { @@ -23,6 +24,9 @@ class RecipeSeeder extends Seeder RecipeIngredient::updateOrCreate(['recipe_id'=>$recipe['id'],'order'=>0,'alternative'=>true,'name'=>'sausage','quantity'=>1.0,'measurement'=>'lb','special_notes'=>'']); RecipeIngredient::updateOrCreate(['recipe_id'=>$recipe['id'],'order'=>0,'alternative'=>false,'name'=>'carrot','quantity'=>1.0,'measurement'=>'lb','special_notes'=>'']); + RecipeCategory::updateOrCreate(['recipe_id'=>$recipe['id'],'name'=>'breakfast']); + + $recipe=Recipe::updateOrCreate(['name'=>'Test2','user_id'=>$first_user['id'],'author'=>'Karen','servings'=>1,'serving_size'=>'1 cup','date_entered'=>now(),'date_modified'=>now(),'instructions'=>'These are instructions']); @@ -31,9 +35,18 @@ class RecipeSeeder extends Seeder $jayne=User::updateOrCreate(['name'=>'Jayne','email'=>'jayne.passmore@actcur.com','password'=>bcrypt('temp'),'created_at'=>NOW()]); + RecipeCategory::updateOrCreate(['recipe_id'=>$recipe['id'],'name'=>'entree']); + RecipeCategory::updateOrCreate(['recipe_id'=>$recipe['id'],'name'=>'mexican']); + + $recipe=Recipe::updateOrCreate(['name'=>'Test3','user_id'=>$jayne['id'],'author'=>'Jayne','servings'=>1,'serving_size'=>'1 cup','date_entered'=>now(),'date_modified'=>now(),'instructions'=>'These are instructions']); RecipeIngredient::updateOrCreate(['recipe_id'=>$recipe['id'],'order'=>0,'alternative'=>false,'name'=>'chicken','quantity'=>1.0,'measurement'=>'lb','special_notes'=>'']); RecipeIngredient::updateOrCreate(['recipe_id'=>$recipe['id'],'order'=>0,'alternative'=>false,'name'=>'carrot','quantity'=>1.0,'measurement'=>'lb','special_notes'=>'']); + + RecipeCategory::updateOrCreate(['recipe_id'=>$recipe['id'],'name'=>'entree']); + RecipeCategory::updateOrCreate(['recipe_id'=>$recipe['id'],'name'=>'mexican']); + RecipeCategory::updateOrCreate(['recipe_id'=>$recipe['id'],'name'=>'something']); + } } diff --git a/resources/views/recipes/index.blade.php b/resources/views/recipes/index.blade.php index 3eff563..245f4db 100644 --- a/resources/views/recipes/index.blade.php +++ b/resources/views/recipes/index.blade.php @@ -1,5 +1,5 @@ @extends('layouts.default') -@extends('content_wrappers.md-10') +@extends('content_wrappers.md-12') @section('title', '| Recipes') @section('heading', 'Recipes') @@ -11,60 +11,82 @@ Name + Categories Created By - Maintainer - Operations + @if(Auth::user()) + Operations + @endif - - - - + + + + @if(Auth::user()) + + @endif @foreach ($recipes as $recipe) {{ $recipe->name }} - {{ $recipe->author }} - {{ $recipe->user ->name}} - - @if(in_array('EditRecipe',$allperms) || ( Auth::user() && $recipe->user_id == Auth::user()->id)) - Edit - @endif - - @if(in_array('DeleteRecipe',$allperms) || ( Auth::user() && $recipe->user_id == Auth::user()->id)) - {!! Form::open(['method' => 'DELETE', 'route' => ['recipes.destroy', $recipe->id] ]) !!} - {!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!} - {!! Form::close() !!} - @endif + + @foreach ($recipe->categories as $category) + {{ $category->name }} + @endforeach + {{ $recipe->author }} + @if(Auth::user()) + + @if(in_array('EditRecipe',$allperms) || $recipe->user_id == Auth::user()->id) + Edit + @endif + + @if(in_array('DeleteRecipe',$allperms) || $recipe->user_id == Auth::user()->id) + {!! Form::open(['method' => 'DELETE', 'route' => ['recipes.destroy', $recipe->id] ]) !!} + {!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!} + {!! Form::close() !!} + @endif + + @endif @endforeach - @if(in_array('Createrecipe',$allperms)) - Add recipe + @if(Auth::user()) + Create New Recipe @endif @endsection @section('scripts') + @endsection +@section('styles') + +@endsection