shareit/app/Http/Controllers/ItemController.php

78 lines
2.1 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers;
use \App\Item;
use \App\User;
2018-09-20 03:18:30 +00:00
use App\FlashMessage as flash;
2021-05-20 23:37:35 +00:00
use Illuminate\Http\Request;
class ItemController extends Controller
{
public function show($id)
{
$item = Item::find($id);
2018-09-20 03:18:30 +00:00
if (!$item || $item->product->user_id != \Auth::id()) {
2021-05-20 23:37:35 +00:00
session()->flash(
flash::DANGER,
\Lang::getFromJson(
"The item doesn't exist."
)
);
2018-09-20 03:18:30 +00:00
return back();
}
$users = $item->users()->get();
$otherItems = Item::where([['product_id', $item->product_id], ['id', '!=', $id]])->get();
return view('item', compact('item', 'otherItems', 'users'));
}
public function index()
{
$items = Item::fromAuthUser()->get();
return view('item.index', compact('items'));
}
/**
* Stores the included item into database
* As the items are included on the Product view,
* it must return to there after inclusion
2018-09-20 03:18:30 +00:00
*
* @return (view) The product view
*/
2018-09-11 23:48:09 +00:00
public function store(Request $request)
{
2018-09-11 23:48:09 +00:00
$request->validate(
[
'item' => 'required',
'product_id' => 'required'
]
);
$authUser = User::loggedIn();
$authUser->items()->create(['name' => request('item'), 'product_id' => request('product_id')]);
2018-09-20 03:18:30 +00:00
return redirect('product/' . request('product_id'));
}
public function patch(Request $request)
{
$request->validate(['item' => 'required', 'name' => 'required']);
$item = User::loggedIn()->items()->find(request('item'));
$item->name = request('name');
$item->save();
2018-09-20 03:18:30 +00:00
return redirect('item/' . request('item'));
}
2018-09-13 00:27:04 +00:00
public function delete(Request $request)
{
$request->validate(['item' => 'required']);
$item = User::loggedIn()
->items()->with('users')->find(request('item'));
2018-09-13 00:27:04 +00:00
$product = $item->product_id;
Item::deleteAndDetach($item);
2018-09-13 00:27:04 +00:00
return redirect('product/' . $product);
}
2018-09-20 03:18:30 +00:00
}