1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Events\ModelWasCreated; |
6
|
|
|
use App\Events\ModelWasDeleted; |
7
|
|
|
use App\Model; |
8
|
|
|
use File; |
9
|
|
|
use Illuminate\Http\Request; |
10
|
|
|
|
11
|
|
|
class ModelController extends Controller |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* ModelController constructor. |
15
|
|
|
*/ |
16
|
8 |
|
public function __construct() |
17
|
|
|
{ |
18
|
8 |
|
$this->middleware('auth'); |
19
|
8 |
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Display a listing of the resource. |
23
|
|
|
* |
24
|
|
|
* @return \Illuminate\Http\Response |
25
|
|
|
*/ |
26
|
1 |
|
public function index() |
27
|
|
|
{ |
28
|
1 |
|
return view('models.index'); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Store a newly created resource in storage. |
33
|
|
|
* |
34
|
|
|
* @param \Illuminate\Http\Request $request |
35
|
|
|
*/ |
36
|
2 |
|
public function store(Request $request) |
37
|
|
|
{ |
38
|
2 |
|
$this->validate($request, [ |
39
|
2 |
|
'name' => 'required|string|max:255', |
40
|
2 |
|
'make_id' => 'required|exists:makes,id', |
41
|
2 |
|
'photo' => 'required|image', |
42
|
2 |
|
]); |
43
|
|
|
$photo = $request->file('photo'); |
44
|
|
|
$fileName = sha1(time().$photo->getClientOriginalName()).'.'.$photo->getClientOriginalExtension(); |
45
|
|
|
$photo->move(config('shop.images.path'), $fileName); |
46
|
|
|
$model = Model::create($request->only([ |
47
|
|
|
'name', |
48
|
|
|
'make_id', |
49
|
|
|
]) + ['photo' => config('shop.images.dir').$fileName]); |
50
|
|
|
event(new ModelWasCreated($model)); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Remove the specified resource from storage. |
55
|
|
|
* |
56
|
|
|
* @param int $id |
57
|
|
|
*/ |
58
|
2 |
|
public function destroy($id) |
59
|
|
|
{ |
60
|
2 |
|
$model = Model::findOrFail($id); |
61
|
2 |
|
File::delete(public_path($model->photo)); |
62
|
2 |
|
event(new ModelWasDeleted($model)); |
63
|
2 |
|
$model->delete(); |
64
|
2 |
|
} |
65
|
|
|
} |
66
|
|
|
|