shareit/app/Http/Controllers/TakeController.php

54 lines
1.1 KiB
PHP
Raw Normal View History

2018-09-13 00:27:35 +00:00
<?php
namespace App\Http\Controllers;
use Auth;
use Lang;
use App\Item;
use App\User;
use App\Events\ReturnItem;
use Illuminate\Http\Request;
2018-09-13 00:27:35 +00:00
/**
* Responsible to Take and Return an Item.
*/
2018-09-13 00:27:35 +00:00
class TakeController extends Controller
{
/**
* The user take an item
*
* @param Request $request The form data
*
* @return home view
*/
2018-09-13 00:27:35 +00:00
public function store(Request $request)
{
$item = User::loggedIn()->items()->find(request('item'));
if ($item->used_by) {
return back()->withErrors(
Lang::getFromJson("This item is already taken")
);
}
$item->used_by = Auth::id();
$item->waiting_user_id = null;
2018-09-13 00:27:35 +00:00
$item->save();
return redirect('home');
}
/**
* User return an item
* Trigger an event: ReturnItem
*
* @param Request $request Form data
*
* @return View home
*/
public function delete(Request $request)
{
$item = User::loggedIn()->items()->find(request('item'));
event(new ReturnItem($item));
$item->returnItem();
return redirect('home');
}
2018-09-13 00:27:35 +00:00
}