2018-09-09 03:26:57 +00:00
< ? php
namespace App\Http\Controllers ;
2018-09-17 03:51:41 +00:00
use \App\Item ;
2018-09-10 04:23:32 +00:00
use \App\User ;
2018-09-17 03:51:41 +00:00
use \App\Product ;
use Illuminate\Http\Request ;
2018-09-09 03:26:57 +00:00
class ProductController extends Controller
{
public function index ()
{
2018-09-19 17:58:10 +00:00
$products = Product :: fromAuthUser () -> get ();
2018-09-10 04:23:32 +00:00
return view ( 'product.index' , compact ( 'products' ));
2018-09-09 03:26:57 +00:00
}
2018-09-10 04:23:32 +00:00
/**
* Stores the included product into database
2018-09-20 00:07:40 +00:00
*
2018-09-10 04:23:32 +00:00
* @ return ( view ) The product view
*/
2018-09-16 23:32:32 +00:00
public function store ( Request $request )
2018-09-10 04:23:32 +00:00
{
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
2018-09-10 04:23:32 +00:00
return redirect ( 'product' );
}
/**
* Delete a specified Product
2018-09-20 00:07:40 +00:00
*
2018-09-10 04:23:32 +00:00
* @ param ( int ) $id The product id
*/
2018-09-16 23:32:32 +00:00
public function delete ( Request $request )
2018-09-10 04:23:32 +00:00
{
2018-09-16 23:32:32 +00:00
$request -> validate ([ 'product' => 'required' ]);
2018-09-19 17:58:10 +00:00
$product = User :: loggedIn ()
2018-09-17 03:51:41 +00:00
-> products () -> with ( 'items' ) -> find ( request ( 'product' ));
2018-09-16 23:32:32 +00:00
//Detach users from this item
2018-09-17 03:51:41 +00:00
foreach ( $product -> items as $item ) {
Item :: deleteAndDetach ( $item );
2018-09-16 23:32:32 +00:00
}
2018-09-17 03:51:41 +00:00
//Delete product
$product -> delete ();
return redirect ( 'product' );
2018-09-16 23:32:32 +00:00
}
public function patch ( Request $request )
{
2018-09-16 23:43:14 +00:00
$request -> validate ([ 'product' => 'required' , 'name' => 'required' ]);
2018-09-19 17:58:10 +00:00
$product = User :: loggedIn () -> products () -> find ( request ( 'product' ));
2018-09-16 23:43:14 +00:00
$product -> name = request ( 'name' );
$product -> url = request ( 'url' );
$product -> save ();
2018-09-20 00:07:40 +00:00
return redirect ( 'product/' . request ( 'product' ));
2018-09-10 04:23:32 +00:00
}
/**
* Show a specified Product
2018-09-20 00:07:40 +00:00
*
2018-09-10 04:23:32 +00:00
* @ param ( int ) $id The product id
2018-09-20 00:07:40 +00:00
*
2018-09-19 18:11:30 +00:00
* @ return \Illuminate\Http\Response
2018-09-10 04:23:32 +00:00
*/
2018-09-09 03:26:57 +00:00
public function show ( $id )
{
2018-09-19 17:58:10 +00:00
$product = Product :: fromAuthUser () -> find ( $id );
2018-09-19 18:11:30 +00:00
if ( ! $product ) {
2018-09-20 00:07:40 +00:00
session () -> flash ( 'danger' , " The product doesn't exist or doesn't belongs to you. " );
2018-09-19 18:11:30 +00:00
return back ();
}
2018-09-11 23:48:09 +00:00
return view ( 'product.show' , compact ( 'product' ));
2018-09-09 03:26:57 +00:00
}
}