From 614dbeda3507d095658c199e105040fb92639c3e Mon Sep 17 00:00:00 2001 From: Bruno Fontes Date: Sun, 16 Sep 2018 20:32:32 -0300 Subject: [PATCH] Adding URL to PRODUCT --- app/Http/Controllers/ProductController.php | 30 +++++++++++++++++-- app/Product.php | 10 +------ ...018_09_08_192040_create_products_table.php | 1 + resources/views/home.blade.php | 8 +++++ 4 files changed, 37 insertions(+), 12 deletions(-) diff --git a/app/Http/Controllers/ProductController.php b/app/Http/Controllers/ProductController.php index 3671d09..0aa0fc3 100644 --- a/app/Http/Controllers/ProductController.php +++ b/app/Http/Controllers/ProductController.php @@ -19,9 +19,10 @@ class ProductController extends Controller * * @return (view) The product view */ - public function store() + public function store(Request $request) { - Product::create(['name' => request('product'), 'user_id' => \Auth::id()]); //Just remember to add the fillable on Model to make this work + $request->validate(['product' => 'required']); + Product::create(['name' => request('product'), 'user_id' => \Auth::id(), 'url' => request('url') ?? '']); //Just remember to add the fillable on Model to make this work return redirect('product'); } @@ -30,8 +31,31 @@ class ProductController extends Controller * * @param (int) $id The product id */ - public function delete($id) + public function delete(Request $request) { + //TODO: Delete all items with all users for the product + $request->validate(['product' => 'required']); + $item = User::find(\Auth::id()) + ->items()->with('users')->find(request('item')); + $product = $item->product_id; + + //Detach users from this item + foreach ($item->users as $user) { + User::findOrFail($user->id)->items()->detach($item->id); + } + + //Delete item + $item->delete(); + return redirect('product/' . $product); + } + + public function patch(Request $request) + { + $request->validate(['item' => 'required', 'name' => 'required']); + $item = User::find(\Auth::id())->items()->find(request('item')); + $item->name = request('name'); + $item->save(); + return redirect('item/'.request('item')); } /** diff --git a/app/Product.php b/app/Product.php index 482e5c4..db0d0e1 100644 --- a/app/Product.php +++ b/app/Product.php @@ -6,7 +6,7 @@ use Illuminate\Database\Eloquent\Model; class Product extends Model { - protected $fillable = ['user_id', 'name']; + protected $fillable = ['user_id', 'name', 'url']; public function items() { @@ -17,12 +17,4 @@ class Product extends Model { return $this->belongsTo(User::class); } - - //Ótima forma de adicionar um item por dentro do produto. Mas não estou usando por não saber como lidar com a table de UsuáriosPorItem - /* - public function addItem($name) - { - $this->items()->create(compact('name')); - } - */ } diff --git a/database/migrations/2018_09_08_192040_create_products_table.php b/database/migrations/2018_09_08_192040_create_products_table.php index dabaa0a..3005d24 100644 --- a/database/migrations/2018_09_08_192040_create_products_table.php +++ b/database/migrations/2018_09_08_192040_create_products_table.php @@ -17,6 +17,7 @@ class CreateProductsTable extends Migration $table->increments('id'); $table->string('name'); $table->integer('user_id'); + $table->string('url'); $table->timestamps(); }); } diff --git a/resources/views/home.blade.php b/resources/views/home.blade.php index 3a26222..6398800 100644 --- a/resources/views/home.blade.php +++ b/resources/views/home.blade.php @@ -18,8 +18,16 @@
@endif
+ @if ($item->product->url) + + @endif + {{$item->name}} ({{$item->product->name}}) + @if ($item->product->url) + + @endif + @if ($item->used_by) @include('home.usedItem') @else