Including the option to add users to items

This commit is contained in:
Bruno F. Fontes 2018-09-13 21:16:16 -03:00
parent d20aec8f3c
commit 3469924cdf
4 changed files with 151 additions and 3 deletions

View File

@ -11,9 +11,10 @@ class ItemController extends Controller
{
$item = Item::find($id);
if (!$item || $item->product->user_id != \Auth::id()) return back();
$users = $item->users()->get();
$otherItems = Item::where([['product_id', $item->product_id], ['id', '!=', $id]])->get();
return view('item', compact('item', 'otherItems'));
return view('item', compact('item', 'otherItems', 'users'));
}
public function index()
@ -39,8 +40,8 @@ class ItemController extends Controller
]
);
$user = \App\User::find(\Auth::id());
$user->items()->create(['name' => request('item'), 'product_id' => request('product_id')]);
$authUser = \App\User::find(\Auth::id());
$authUser->items()->create(['name' => request('item'), 'product_id' => request('product_id')]);
return redirect('product/'.request('product_id'));
}

View File

@ -0,0 +1,102 @@
<?php
namespace App\Http\Controllers;
use \App\User;
use \App\Item;
use \App\Product;
use Illuminate\Http\Request;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, ['email' => 'required', 'item_id' => 'required']);
$user = User::where('email', request('email'))->get();
if (count($user) == 0) {
return back()->withErrors("The e-mail address is not registered yet.");
}
$item = Item::findOrFail(request('item_id'));
if ($item->product->user_id == \Auth::id()) {
User::findOrFail($user[0]->id)->items()->attach(request('item_id'));
//$user->items()->attach(request('item_id'));
} else {
return back()->withErrors("You cannot add a user to a product that is not yourse.");
}
return back();
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}

View File

@ -28,6 +28,48 @@
</ul>
</div>
</div>
<div class="card mt-4">
<div class="card-header">
Users of this item
</div>
<div class="card-body">
<strong>Users that has access to this item:</strong>
<ul>
@forelse ($users as $user)
<hr>
<li>
{{ $user->name }} ({{ $user->email}})
<div class="form-inline float-right">
<form method="POST" action="/user" class="form-inline"> <div class="form-group">
{{ csrf_field() }}
@method('DELETE')
<input type="hidden" class="form-control" name="user_id" id="user_id" value="{{$user->id}}">
<input type="hidden" name="item_id" id="item_id" value="{{ $item['id'] }}">
<button type="submit" class="btn-xm btn-danger">Delete</button>
</form></div>
</li>
@empty
<p>There are no items yet. Include one with the form above.</p>
@endforelse
</ul>
<div class="mt-4">
<strong>Add user</strong>
<form method="POST" action="/user" class="form-inline">
<div class="form-group">
{{ csrf_field() }}
<div class="col"><label for="name">E-mail: </label></div>
<div class="col-6"><input type="email" class="form-control" name="email" id="email" placeholder="name@domain.com" required></div>
<input type="hidden" name="item_id" id="item_id" value="{{ $item['id'] }}" required>
<div class="col ml-3"><button type="submit" class="btn btn-primary">Insert</button></div>
</div>
</form>
</div>
@include ('layouts.errors')
</div>
</div>
<div class="float-right mt-2 mr-4"><a href="{{ URL::previous() }}">BACK</a></div>
</div>
</div>

View File

@ -27,6 +27,9 @@ Route::delete('/item', 'ItemController@delete')->middleware('auth');
Route::post('/take', 'TakeController@store')->middleware('auth');
Route::post('/return', 'ReturnController@store')->middleware('auth');
Route::post('/user', 'UserController@store')->middleware('auth');
Route::delete('/user', 'UserController@delete')->middleware('auth');
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home')->middleware('auth');