Added categories along w/ category autocomplete/filter

This commit is contained in:
Beth Parker 2022-02-11 23:02:48 -06:00
parent a6e28a64aa
commit 7ee58d18fe
6 changed files with 124 additions and 31 deletions

View file

@ -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){

View file

@ -28,4 +28,9 @@ class Recipe extends Model
{
return $this->hasMany('App\RecipeIngredient');
}
public function categories()
{
return $this->hasMany('App\RecipeCategory');
}
}

17
app/RecipeCategory.php Normal file
View file

@ -0,0 +1,17 @@
<?php
namespace App;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class RecipeCategory extends Model
{
use HasFactory;
protected $fillable = ['recipe_id','name'];
public function recipe()
{
return $this->belongsTo('App\Recipe');
}
}

View file

@ -0,0 +1,35 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateRecipeCategoryTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('recipe_categories', function (Blueprint $table) {
$table->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');
}
}

View file

@ -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']);
}
}

View file

@ -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 @@
<thead>
<tr>
<th>Name</th>
<th>Categories</th>
<th>Created By</th>
<th>Maintainer</th>
<th>Operations</th>
@if(Auth::user())
<th>Operations</th>
@endif
</tr>
</thead>
<tbody id="filteredTable">
<tr>
<td><input id="nameFilter" class="filter" type="text" placeholder="Search.."></td>
<td><input id="authorFilter" class="filter" type="text" placeholder="Search.."></td>
<td><input id="userFilter" class="filter" type="text" placeholder="Search.."></td>
<td class='operations'></td>
<td><input id="nameFilter" class="filter form-control" type="text" placeholder="Search.."></td>
<td><input id="categoryFilter" class="filter form-control" type="text" placeholder="Search.."></td>
<td><input id="authorFilter" class="filter form-control" type="text" placeholder="Search.."></td>
@if(Auth::user())
<td class='operations'></td>
@endif
</tr>
@foreach ($recipes as $recipe)
<tr class="rows">
<td class="name"><a href="{{ route('recipes.show', $recipe->id) }}">{{ $recipe->name }}</a></td>
<td class="author">{{ $recipe->author }}</td>
<td class="user">{{ $recipe->user ->name}}</td>
<td>
@if(in_array('EditRecipe',$allperms) || ( Auth::user() && $recipe->user_id == Auth::user()->id))
<a href="{{ route('recipes.edit', $recipe->id) }}" class="btn btn-info pull-left" style="margin-right: 3px;">Edit</a>
@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
<td class="categories">
@foreach ($recipe->categories as $category)
{{ $category->name }}
@endforeach
</td>
<td class="author">{{ $recipe->author }}</td>
@if(Auth::user())
<td>
@if(in_array('EditRecipe',$allperms) || $recipe->user_id == Auth::user()->id)
<a href="{{ route('recipes.edit', $recipe->id) }}" class="btn btn-info pull-left" style="margin-right: 3px;">Edit</a>
@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
</td>
@endif
</tr>
@endforeach
</tbody>
</table>
</div>
@if(in_array('Createrecipe',$allperms))
<a href="{{ route('recipes.create') }}" class="btn btn-success">Add recipe</a>
@if(Auth::user())
<a href="{{ route('recipes.create') }}" class="btn btn-success">Create New Recipe</a>
@endif
@endsection
@section('scripts')
<script type="text/javascript" src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$(document).ready(function(){
$(".filter").on("keyup", function() {
var categories = @json($categories);
$(document).on("click",function(){filter();});
$(".filter").on("input", function() {filter();});
function filter(){
var name = $("#nameFilter").val().toLowerCase();
var category = $("#categoryFilter").val().toLowerCase();
var author = $("#authorFilter").val().toLowerCase();
var user = $("#userFilter").val().toLowerCase();
$("#filteredTable tr.rows").filter(function() {
$matchname=$(".name",this).text().toLowerCase().indexOf(name) > -1;
$matchauthor=$(".author",this).text().toLowerCase().indexOf(author) > -1;
$matchuser=$(".user",this).text().toLowerCase().indexOf(user) > -1;
$(this).toggle($matchname && $matchauthor && $matchuser);
matchname=$(".name",this).text().toLowerCase().indexOf(name) > -1;
matchcategory=$(".categories",this).text().toLowerCase().indexOf(category) > -1;
matchauthor=$(".author",this).text().toLowerCase().indexOf(author) > -1;
$(this).toggle(matchname && matchcategory && matchauthor);
});
}
$("#categoryFilter").autocomplete({
source: categories
});
});
</script>
@endsection
@section('styles')
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
@endsection