Added Delete PRODUCT with Items and dettaching from users

This commit is contained in:
Bruno F. Fontes 2018-09-17 00:51:41 -03:00
parent 707dd1654a
commit c38744e392
4 changed files with 21 additions and 27 deletions

View File

@ -58,17 +58,11 @@ class ItemController extends Controller
public function delete(Request $request) public function delete(Request $request)
{ {
$request->validate(['item' => 'required']); $request->validate(['item' => 'required']);
$item = User::find(\Auth::id()) $item = User::findOrFail(\Auth::id())
->items()->with('users')->find(request('item')); ->items()->with('users')->find(request('item'));
$product = $item->product_id; $product = $item->product_id;
//Detach users from this item Item::deleteAndDetach($item);
foreach ($item->users as $user) {
User::findOrFail($user->id)->items()->detach($item->id);
}
//Delete item
$item->delete();
return redirect('product/' . $product); return redirect('product/' . $product);
} }
} }

View File

@ -2,9 +2,10 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use Illuminate\Http\Request; use \App\Item;
use \App\Product;
use \App\User; use \App\User;
use \App\Product;
use Illuminate\Http\Request;
class ProductController extends Controller class ProductController extends Controller
{ {
@ -33,20 +34,18 @@ class ProductController extends Controller
*/ */
public function delete(Request $request) public function delete(Request $request)
{ {
//TODO: Delete all items with all users for the product
$request->validate(['product' => 'required']); $request->validate(['product' => 'required']);
$item = User::findOrFail(\Auth::id()) $product = User::findOrFail(\Auth::id())
->items()->with('users')->find(request('item')); ->products()->with('items')->find(request('product'));
$product = $item->product_id;
//Detach users from this item //Detach users from this item
foreach ($item->users as $user) { foreach ($product->items as $item) {
User::findOrFail($user->id)->items()->detach($item->id); Item::deleteAndDetach($item);
} }
//Delete item //Delete product
$item->delete(); $product->delete();
return redirect('product/' . $product); return redirect('product');
} }
public function patch(Request $request) public function patch(Request $request)

View File

@ -18,13 +18,14 @@ class Item extends Model
return $this->belongsToMany(User::class); return $this->belongsToMany(User::class);
} }
public function free($product_id, $user_id) public static function deleteAndDetach($item)
{ {
return $query->where([ //Detach users from this item
['user_id', $user_id], foreach ($item->users as $user) {
['product_id', $product_id], User::findOrFail($user->id)->items()->detach($item->id);
['used_by', ''], }
]);
}
//Delete item
$item->delete();
}
} }

View File

@ -16,7 +16,7 @@
</div> </div>
<div class="modal-footer"> <div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button> <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<form action="/delete" method="POST"> <form action="/product" method="POST">
@method('DELETE') @method('DELETE')
{{ csrf_field() }} {{ csrf_field() }}
<input type="hidden" name="product" value="{{$product->id}}"> <input type="hidden" name="product" value="{{$product->id}}">