Added Recipe index page w/ filter

This commit is contained in:
Beth Parker 2022-02-07 21:50:25 -06:00
parent 2a559cb381
commit f4e9e87335
10 changed files with 156 additions and 22 deletions

View file

@ -0,0 +1,28 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Recipe;
use App\RecipeIngredient;
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);
}
public function show($id)
{
$recipe=Recipe::with('user')->with('ingredients')->findOrFail($id);
return $ingredients;
}
public function edit($id){
$recipe=Recipe::with('user')->with('ingredients')->findOrFail($id);
$ingredients=RecipeIngredient::get()->sortby('name')->pluck('name')->unique();
return $ingredients;
}
}

View file

@ -10,6 +10,11 @@ class Recipe extends Model
use HasFactory; use HasFactory;
protected $fillable = ['name','maintainer','author','servings','date_entered','date_modified','instructions']; protected $fillable = ['name','maintainer','author','servings','date_entered','date_modified','instructions'];
public function user()
{
return $this->belongsTo('App\User');
}
public function ingredients() public function ingredients()
{ {
return $this->hasMany('App\RecipeIngredient'); return $this->hasMany('App\RecipeIngredient');

View file

@ -67,4 +67,8 @@ class User extends Authenticatable
} }
return false; return false;
} }
public function recipes(){
return $this->hasMany('App\Recipe');
}
} }

View file

@ -1,17 +0,0 @@
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
class RecipeFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
}
}

View file

@ -16,7 +16,7 @@ class CreateRecipesTable extends Migration
Schema::create('recipes', function (Blueprint $table) { Schema::create('recipes', function (Blueprint $table) {
$table->id(); $table->id();
$table->string('name'); $table->string('name');
$table->unsignedInteger('maintainer'); $table->unsignedInteger('user_id');
$table->string('author'); $table->string('author');
$table->unsignedInteger('servings'); $table->unsignedInteger('servings');
$table->datetime('date_entered'); $table->datetime('date_entered');
@ -24,7 +24,7 @@ class CreateRecipesTable extends Migration
$table->longtext('instructions'); $table->longtext('instructions');
$table->timestamps(); $table->timestamps();
$table->foreign('maintainer')->references('id')->on('users'); $table->foreign('user_id')->references('id')->on('users');
}); });
} }

View file

@ -4,7 +4,7 @@ use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\Schema;
class CreateRecipeIngredientTable extends Migration class CreateRecipeIngredientsTable extends Migration
{ {
/** /**
* Run the migrations. * Run the migrations.
@ -13,7 +13,7 @@ class CreateRecipeIngredientTable extends Migration
*/ */
public function up() public function up()
{ {
Schema::create('recipe_ingredient', function (Blueprint $table) { Schema::create('recipe_ingredients', function (Blueprint $table) {
$table->id(); $table->id();
$table->unsignedInteger('recipe_id'); $table->unsignedInteger('recipe_id');
$table->unsignedInteger('order'); $table->unsignedInteger('order');
@ -35,6 +35,6 @@ class CreateRecipeIngredientTable extends Migration
*/ */
public function down() public function down()
{ {
Schema::dropIfExists('recipe_ingredient'); Schema::dropIfExists('recipe_ingredients');
} }
} }

View file

@ -0,0 +1,39 @@
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use App\User;
use App\Recipe;
use App\RecipeIngredient;
class RecipeSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$first_user = User::orderBy('id')->first();
$recipe=Recipe::updateOrCreate(['name'=>'Test','user_id'=>$first_user['id'],'author'=>'Jayne','servings'=>1,'date_entered'=>now(),'date_modified'=>now(),'instructions'=>'These are instructions']);
RecipeIngredient::updateOrCreate(['recipe_id'=>$recipe['id'],'order'=>0,'alternative'=>false,'name'=>'hamburger','quantity'=>1.0,'measurement'=>'lb','special_notes'=>'']);
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'=>'']);
$recipe=Recipe::updateOrCreate(['name'=>'Test2','user_id'=>$first_user['id'],'author'=>'Karen','servings'=>1,'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'=>'']);
$jayne=User::updateOrCreate(['name'=>'Jayne','email'=>'jayne.passmore@actcur.com','password'=>bcrypt('temp'),'created_at'=>NOW()]);
$recipe=Recipe::updateOrCreate(['name'=>'Test3','user_id'=>$jayne['id'],'author'=>'Jayne','servings'=>1,'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'=>'']);
}
}

View file

@ -265,3 +265,7 @@ list {
.fix-spacing{ .fix-spacing{
padding-top: 30px; padding-top: 30px;
} }
.operations{
width:132px;
}

View file

@ -0,0 +1,70 @@
@extends('layouts.default')
@extends('content_wrappers.md-10')
@section('title', '| Recipes')
@section('heading', 'Recipes')
@section('content')
<div class="table-responsive">
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Name</th>
<th>Created By</th>
<th>Maintainer</th>
<th>Operations</th>
</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>
</tr>
@foreach ($recipes as $recipe)
<tr class="rows">
<td class="name">{{ $recipe->name }}</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>
</tr>
@endforeach
</tbody>
</table>
</div>
@if(in_array('Createrecipe',$allperms))
<a href="{{ route('recipes.create') }}" class="btn btn-success">Add recipe</a>
@endif
@endsection
@section('scripts')
<script>
$(document).ready(function(){
$(".filter").on("keyup", function() {
var name = $("#nameFilter").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);
});
});
});
</script>
@endsection

View file

@ -37,4 +37,5 @@ Route::group(['middleware' => 'permissions:SHARE'], function(){
Route::resource('users','UserController'); Route::resource('users','UserController');
Route::resource('roles','RoleController'); Route::resource('roles','RoleController');
Route::resource('recipes','RecipeController');
}); });