1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Products; |
6
|
|
|
use App\ProductsCategories; |
7
|
|
|
use Illuminate\Http\Request; |
8
|
|
|
|
9
|
|
|
use App\Http\Requests; |
10
|
|
|
|
11
|
|
|
class ProductsController extends Controller |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* ProductsController constructor. |
15
|
|
|
*/ |
16
|
|
|
public function __construct() |
17
|
|
|
{ |
18
|
|
|
$this->middleware('auth'); |
19
|
|
|
$this->middleware('lang'); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
View Code Duplication |
public function index() |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
$data['products'] = Products::with('category')->paginate(10); |
|
|
|
|
25
|
|
|
$data['category'] = ProductsCategories::with('products')->paginate(10)->sortBy("name")->all(); |
26
|
|
|
|
27
|
|
|
return view('products.index', $data); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function categories() |
31
|
|
|
{ |
32
|
|
|
$data['category'] = ProductsCategories::with('products')->paginate(10)->sortBy("name")->all(); |
|
|
|
|
33
|
|
|
return view('products.categories', $data); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function edit_categories($id) |
37
|
|
|
{ |
38
|
|
|
$data['category'] = ProductsCategories::find($id); |
|
|
|
|
39
|
|
|
return view('products.edit_category', $data); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function remove_category($id) |
43
|
|
|
{ |
44
|
|
|
$category = ProductsCategories::find($id); |
45
|
|
|
$category->delete(); |
46
|
|
|
|
47
|
|
|
session()->flash('message', trans('products.removed_category')); |
48
|
|
|
return redirect()->back(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* [METHOD]: Add a new product in the database. |
54
|
|
|
* |
55
|
|
|
* @url:platform POST: /products/save |
56
|
|
|
* @see:phpunit TODO: write phpunit test in a later version. |
57
|
|
|
* |
58
|
|
|
* @param Requests\ProductValidator $input |
59
|
|
|
* @return \Illuminate\Http\RedirectResponse |
60
|
|
|
*/ |
61
|
|
|
public function store(Requests\ProductValidator $input) |
62
|
|
|
{ |
63
|
|
|
$this->validate($input, [ |
64
|
|
|
'name' => 'required', |
65
|
|
|
'category' => 'required', |
66
|
|
|
]); |
67
|
|
|
|
68
|
|
|
$product = new Products(); |
69
|
|
|
$product->name = $input->name; |
|
|
|
|
70
|
|
|
|
71
|
|
|
$product->category()->associate($input->category); |
|
|
|
|
72
|
|
|
$product->save(); |
73
|
|
|
|
74
|
|
|
session()->flash('message', trans('products.saved')); |
75
|
|
|
return redirect()->back(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* [METHOD]: Add a new product category in the database. |
80
|
|
|
* |
81
|
|
|
* @url:platform POST: /products/categories/save |
82
|
|
|
* |
83
|
|
|
* @param Requests\ProductValidator $input |
84
|
|
|
* @return \Illuminate\Http\RedirectResponse |
85
|
|
|
*/ |
86
|
|
|
public function saveCat(Request $input) |
87
|
|
|
{ |
88
|
|
|
$this->validate($input, [ |
89
|
|
|
'name' => 'required|unique:products_categories' |
90
|
|
|
]); |
91
|
|
|
|
92
|
|
|
$category = new ProductsCategories(); |
93
|
|
|
$category->name = $input->name; |
|
|
|
|
94
|
|
|
$category->save(); |
95
|
|
|
|
96
|
|
|
session()->flash('message', trans('products.category_saved')); |
97
|
|
|
return redirect()->back(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
public function remove($id) |
102
|
|
|
{ |
103
|
|
|
$product = Products::find($id); |
104
|
|
|
$product->delete(); |
105
|
|
|
|
106
|
|
|
session()->flash('message', trans('products.removed')); |
107
|
|
|
return redirect()->back(); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.