1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
|
7
|
|
|
use App\Http\Requests; |
8
|
|
|
|
9
|
|
|
use Session; |
10
|
|
|
|
11
|
|
|
use App\News; |
12
|
|
|
|
13
|
|
|
class NewsController extends Controller |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Display a listing of the resource. |
17
|
|
|
* |
18
|
|
|
* @return \Illuminate\Http\Response |
19
|
|
|
*/ |
20
|
|
|
public function index() |
21
|
|
|
{ |
22
|
|
|
//$news = News::all(); |
|
|
|
|
23
|
|
|
|
24
|
|
|
// using paginate function to show 3 news items per page |
25
|
|
|
$itemsPerPage = 3; |
26
|
|
|
$news = News::orderBy('created_at', 'desc')->paginate($itemsPerPage); |
27
|
|
|
|
28
|
|
|
return view('news.index', array('news' => $news, 'title' => 'News Display')); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Show the form for creating a new resource. |
33
|
|
|
* |
34
|
|
|
* @return \Illuminate\Http\Response |
35
|
|
|
*/ |
36
|
|
|
public function create() |
37
|
|
|
{ |
38
|
|
|
return view('news.create', array('title' => 'Add News')); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Store a newly created resource in storage. |
43
|
|
|
* |
44
|
|
|
* @param \Illuminate\Http\Request $request |
45
|
|
|
* @return \Illuminate\Http\Response |
46
|
|
|
*/ |
47
|
|
|
public function store(Request $request) |
48
|
|
|
{ |
49
|
|
|
$this->validate($request, array( |
50
|
|
|
'title' => 'required', |
51
|
|
|
'slug' => 'required', |
52
|
|
|
'short_description' => 'required', |
53
|
|
|
'full_content' => 'required', |
54
|
|
|
) |
55
|
|
|
); |
56
|
|
|
|
57
|
|
|
$input = $request->all(); |
58
|
|
|
//dd($input); // dd() helper function is print_r alternative |
|
|
|
|
59
|
|
|
|
60
|
|
|
News::create($input); |
61
|
|
|
|
62
|
|
|
Session::flash('flash_message', 'News added successfully!'); |
63
|
|
|
|
64
|
|
|
//return redirect()->back(); |
|
|
|
|
65
|
|
|
//return redirect('news'); |
|
|
|
|
66
|
|
|
return redirect()->route('news.index'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Display the specified resource. |
71
|
|
|
* |
72
|
|
|
* @param string $slug |
73
|
|
|
* @return \Illuminate\Http\Response |
74
|
|
|
*/ |
75
|
|
|
public function show($slug) |
76
|
|
|
{ |
77
|
|
|
$news = News::where('slug', $slug)->first(); |
78
|
|
|
return view('news.show', array('news' => $news)); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Show the form for editing the specified resource. |
83
|
|
|
* |
84
|
|
|
* @param int $id |
85
|
|
|
* @return \Illuminate\Http\Response |
86
|
|
|
*/ |
87
|
|
|
public function edit($id) |
88
|
|
|
{ |
89
|
|
|
$news = News::findOrFail($id); |
90
|
|
|
|
91
|
|
|
return view('news.edit', array('news' => $news, 'title' => 'Edit News')); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Update the specified resource in storage. |
96
|
|
|
* |
97
|
|
|
* @param \Illuminate\Http\Request $request |
98
|
|
|
* @param int $id |
99
|
|
|
* @return \Illuminate\Http\Response |
100
|
|
|
*/ |
101
|
|
|
public function update(Request $request, $id) |
102
|
|
|
{ |
103
|
|
|
$news = News::findOrFail($id); |
104
|
|
|
|
105
|
|
|
$this->validate($request, array( |
106
|
|
|
'title' => 'required', |
107
|
|
|
'slug' => 'required', |
108
|
|
|
'short_description' => 'required', |
109
|
|
|
'full_content' => 'required', |
110
|
|
|
) |
111
|
|
|
); |
112
|
|
|
|
113
|
|
|
$input = $request->all(); |
114
|
|
|
|
115
|
|
|
$news->fill($input)->save(); |
116
|
|
|
|
117
|
|
|
Session::flash('flash_message', 'News updated successfully!'); |
118
|
|
|
|
119
|
|
|
return redirect()->back(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Remove the specified resource from storage. |
124
|
|
|
* |
125
|
|
|
* @param int $id |
126
|
|
|
* @return \Illuminate\Http\Response |
127
|
|
|
*/ |
128
|
|
|
public function destroy($id) |
129
|
|
|
{ |
130
|
|
|
$news = News::findOrFail($id); |
131
|
|
|
|
132
|
|
|
$news->delete(); |
133
|
|
|
|
134
|
|
|
Session::flash('flash_message', 'News deleted successfully!'); |
135
|
|
|
|
136
|
|
|
return redirect()->route('news.index'); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.