shareit/app/Http/Controllers/ProductController.php

72 lines
2.0 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use \App\Product;
use \App\User;
class ProductController extends Controller
{
public function index()
{
$products = Product::where('user_id', \Auth::id())->get();
return view('product.index', compact('products'));
}
/**
* Stores the included product into database
*
* @return (view) The product view
*/
2018-09-16 23:32:32 +00:00
public function store(Request $request)
{
2018-09-16 23:32:32 +00:00
$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');
}
/**
* Delete a specified Product
*
* @param (int) $id The product id
*/
2018-09-16 23:32:32 +00:00
public function delete(Request $request)
{
2018-09-16 23:32:32 +00:00
//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'));
}
/**
* Show a specified Product
*
* @param (int) $id The product id
*/
public function show($id)
{
$product = Product::where('user_id', \Auth::id())->find($id);
2018-09-11 23:48:09 +00:00
return view('product.show', compact('product'));
}
}