From 25cff75c4859d59c908e8a59369c94f6b1d3b971 Mon Sep 17 00:00:00 2001 From: Bruno Fontes Date: Mon, 10 Sep 2018 01:23:32 -0300 Subject: [PATCH] Including Items and Products views and controllers --- app/Http/Controllers/HomeController.php | 3 +- app/Http/Controllers/ItemController.php | 2 +- app/Http/Controllers/ProductController.php | 47 +++++++++++++++++++--- app/Item.php | 3 +- app/Product.php | 1 + resources/views/home.blade.php | 7 +++- resources/views/product/index.blade.php | 41 +++++++++++++++++++ resources/views/product/show.blade.php | 43 ++++++++++++++++++++ routes/web.php | 11 ++--- 9 files changed, 144 insertions(+), 14 deletions(-) create mode 100644 resources/views/product/index.blade.php create mode 100644 resources/views/product/show.blade.php diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php index a3af7dd..e8c7929 100644 --- a/app/Http/Controllers/HomeController.php +++ b/app/Http/Controllers/HomeController.php @@ -23,6 +23,7 @@ class HomeController extends Controller */ public function index() { - return view('home'); + $items = \App\Item::where('userID'); + return view('home', compact('items')); } } diff --git a/app/Http/Controllers/ItemController.php b/app/Http/Controllers/ItemController.php index c8d9eda..1875944 100644 --- a/app/Http/Controllers/ItemController.php +++ b/app/Http/Controllers/ItemController.php @@ -17,7 +17,7 @@ class ItemController extends Controller public function show($id) { - $item = Item::find($id); + $item = Item::where([['id', $id], ['userID', \Auth::id()]]); return view('item', compact('item')); } } diff --git a/app/Http/Controllers/ProductController.php b/app/Http/Controllers/ProductController.php index 2f13d44..b7d7647 100644 --- a/app/Http/Controllers/ProductController.php +++ b/app/Http/Controllers/ProductController.php @@ -4,20 +4,57 @@ namespace App\Http\Controllers; use Illuminate\Http\Request; use \App\Product; +use \App\User; class ProductController extends Controller { // public function index() { - //$product = Product::all(); - return view('product'); + $products = Product::where('adminID', \Auth::id())->get(); + return view('product.index', compact('products')); } + /** + * Stores the included product into database + * + * @return (view) The product view + */ + public function store() + { + /* + $product = new Product; + $product->name = request('product'); + $product->adminID = $userID; + $product->save(); + */ + Product::create(['name' => request('product'), 'adminID' => \Auth::id()]); //Just remember to add the fillable on Model to make this work + + return redirect('product'); + } + + /** + * Delete a specified Product + * + * @param (int) $id The product id + */ + public function delete($id) + { + } + + /** + * Show a specified Product + * + * @param (int) $id The product id + */ public function show($id) { - //$productID = Product::find($id); - $productID = $id; - return view('product', compact('productID')); + $product = Product::find($id); + $items = \DB::table('items') + ->join('products', 'items.productID', '=', 'products.id') + ->where('products.adminID', \Auth::id()) + ->select('items.*', 'products.*') + ->get(); + return view('product.show', compact('items', 'product')); } } diff --git a/app/Item.php b/app/Item.php index bd56790..f2780bd 100644 --- a/app/Item.php +++ b/app/Item.php @@ -7,9 +7,10 @@ use Illuminate\Database\Eloquent\Model; class Item extends Model { // - public function free($productID) + public function free($productID, $userID) { return $query->where([ + ['userID', $userID], ['productID', $productID], ['usedBy', ''], ]); diff --git a/app/Product.php b/app/Product.php index 8dee5b2..afa05e9 100644 --- a/app/Product.php +++ b/app/Product.php @@ -6,5 +6,6 @@ use Illuminate\Database\Eloquent\Model; class Product extends Model { + protected $fillable = ['adminID', 'name']; // } diff --git a/resources/views/home.blade.php b/resources/views/home.blade.php index 05dfca9..e05d426 100644 --- a/resources/views/home.blade.php +++ b/resources/views/home.blade.php @@ -14,7 +14,12 @@ @endif - You are logged in! +

Your itens

+ @forelse ($items as $item) +
  • {{$item.name}}
  • + @empty +

    There are no items for you yet. Include one here.

    + @endforelse diff --git a/resources/views/product/index.blade.php b/resources/views/product/index.blade.php new file mode 100644 index 0000000..5c568d0 --- /dev/null +++ b/resources/views/product/index.blade.php @@ -0,0 +1,41 @@ +@extends('layouts.app') + +@section('content') +
    +
    +
    + +
    +
    + {{ csrf_field() }} +
    +
    +
    +
    +
    +


    + +
    +
    Products
    + +
    + @if (session('status')) + + @endif + +
      + @forelse ($products as $product) +
    • {{$product['name']}}
    • + @empty +

      There are no products yet. Include one with the form above.

      + @endforelse +
    + +
    +
    +
    +
    +
    +@endsection diff --git a/resources/views/product/show.blade.php b/resources/views/product/show.blade.php new file mode 100644 index 0000000..7de5873 --- /dev/null +++ b/resources/views/product/show.blade.php @@ -0,0 +1,43 @@ +@extends('layouts.app') + +@section('content') +
    +
    +
    + +
    +
    + {{ csrf_field() }} +
    +
    +
    +
    +
    +


    + +
    +
    + Product: {{$product['name']}} + Edit - Delete +
    + +
    + @if (session('status')) + + @endif + +
      + @forelse ($items as $item) +
    • {{$item['name']}}
    • + @empty +

      There are no items yet. Include one with the form above.

      + @endforelse +
    + +
    +
    +
    +
    +
    +@endsection diff --git a/routes/web.php b/routes/web.php index f035bbe..4cac891 100644 --- a/routes/web.php +++ b/routes/web.php @@ -15,10 +15,11 @@ Route::get('/', function () { return view('welcome'); }); -Route::get('/product', 'ProductController@index'); -Route::get('/product/{product}', 'ProductController@show'); -Route::get('/item', 'ItemController@index'); -Route::get('/item/{item}', 'ItemController@show'); +Route::get('/product', 'ProductController@index')->middleware('auth'); +Route::get('/product/{product}', 'ProductController@show')->middleware('auth'); +Route::post('/product', 'ProductController@store')->middleware('auth'); +Route::get('/item', 'ItemController@index')->middleware('auth'); +Route::get('/item/{item}', 'ItemController@show')->middleware('auth'); Auth::routes(); -Route::get('/home', 'HomeController@index')->name('home'); +Route::get('/home', 'HomeController@index')->name('home')->middleware('auth');