Skip to content

Commit be61ea8

Browse files
committed
Added: List, Create, Edite, Delete Categories
1 parent 9bb1d01 commit be61ea8

16 files changed

+530
-200
lines changed

.idea/workspace.xml

Lines changed: 189 additions & 188 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/Http/Controllers/AdminController.php

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Http\Controllers;
44

55
use App\Category;
6+
use App\Http\Requests\CategoryCreateRequest;
67
use App\Http\Requests\EditUserRequest;
78
use App\Http\Requests\PostCreateRequest;
89
use App\Http\Requests\UsersRequest;
@@ -260,23 +261,101 @@ public function storePost(PostCreateRequest $request){
260261
}
261262

262263
public function editPost($id){
263-
264+
$post = Post::findOrFail($id);
265+
$categories = Category::lists('name', 'id');
266+
return view('admin.posts.edit', compact(['post', 'categories']));
264267
}
265268

266269
public function updatePost(Request $request, $id){
270+
$post = Post::findOrFail($id);
271+
272+
if($request->has('category_id')){
273+
if(!empty($request->input('category_id'))){
274+
$post->category_id = $request->input('category_id');
275+
}
276+
}
277+
278+
if($request->has('title')){
279+
if(!empty($request->input('title'))){
280+
$post->title = $request->input('title');
281+
}
282+
}
283+
284+
if($request->has('body')){
285+
if(!empty($request->input('body'))){
286+
$post->body = $request->input('body');
287+
}
288+
}
289+
290+
if($request){
291+
if($request->hasFile('photo_id')){
292+
if(!empty($post->photo->path)){
293+
if(file_exists(public_path().$post->photo->path)){
294+
unlink(public_path().$post->photo->path);
295+
}
296+
}
297+
$photo = $post->photo ? $post->photo : new Photo();
298+
$photo->path = time().$request->file('photo_id')->getClientOriginalName();
299+
$request->file('photo_id')->move('image',$photo->path);
300+
301+
$photo->save();
267302

303+
$post->photo_id = $photo->id;
304+
}
305+
306+
}
307+
308+
$post->save();
309+
return redirect('/admin/posts');
268310
}
269311

312+
public function deletePost($id){
313+
$post = Post::findOrFail($id);
270314

271-
public function categories(){
315+
if($post->photo){
316+
if(file_exists(public_path().$post->photo->path)){
317+
unlink(public_path().$post->photo->path);
318+
}
319+
$post->photo->delete();
320+
}
321+
322+
$post->delete();
272323

324+
redirect('/admin/posts');
325+
}
326+
327+
public function categories(){
328+
$categories = Category::all();
329+
return view('admin.categories.index',compact('categories'));
273330
}
274331

275332
public function createCategory(){
333+
return view('admin.categories.create');
334+
}
276335

336+
public function storeCategory(CategoryCreateRequest $request){
337+
$category = new Category();
338+
$category->name = $request->input('name');
339+
$category->save();
340+
341+
return redirect('/admin/categories');
342+
}
343+
344+
public function editCategory($id){
345+
$category = Category::findOrFail($id);
346+
return view('admin.categories.edit',compact('category'));
277347
}
278348

279-
public function storeCategory(){
349+
public function updateCategory(CategoryCreateRequest $request, $id){
350+
$category = Category::findOrFail($id);
351+
$category->name = $request->input('name');
352+
$category->save();
353+
return redirect('admin/categories');
354+
}
280355

356+
public function destroyCategory($id){
357+
$category = Category::findOrFail($id);
358+
$category->delete();
359+
return redirect('admin/categories');
281360
}
282361
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace App\Http\Requests;
4+
5+
use App\Http\Requests\Request;
6+
7+
class CategoryCreateRequest extends Request
8+
{
9+
/**
10+
* Determine if the user is authorized to make this request.
11+
*
12+
* @return bool
13+
*/
14+
public function authorize()
15+
{
16+
return true;
17+
}
18+
19+
/**
20+
* Get the validation rules that apply to the request.
21+
*
22+
* @return array
23+
*/
24+
public function rules()
25+
{
26+
return [
27+
'name'=>'required',
28+
];
29+
}
30+
}

app/Http/routes.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,17 @@
3535
Route::get ('/admin/posts/create' , 'AdminController@createPost' );
3636
Route::post ('/admin/posts/store' , 'AdminController@storePost' );
3737
Route::get ('/admin/posts/edit/{id}' , 'AdminController@editPost' );
38-
Route::post ('/admin/posts/update/{id}' , 'AdminController@updatePost' );
38+
Route::patch ('/admin/posts/update/{id}' , 'AdminController@updatePost' );
3939
Route::delete ('/admin/posts/delete/{id}' , 'AdminController@deletePost' );
4040

4141

4242
Route::get ('/admin/categories' , 'AdminController@categories' );
43-
Route::get ('/admin/categories/create' , 'AdminController@categoryCreate' );
44-
Route::post ('/admin/categories/store' , 'AdminController@categoryStore' );
43+
Route::get ('/admin/categories/create' , 'AdminController@createCategory' );
44+
Route::post ('/admin/categories/store' , 'AdminController@storeCategory' );
45+
Route::get ('/admin/categories/edit/{id}', 'AdminController@editCategory' );
46+
Route::patch ('/admin/categories/update/{id}','AdminController@updateCategory' );
47+
Route::delete ('/admin/categories/destroy/{id}','AdminController@destroyCategory' );
48+
4549
});
4650

4751
Route::get ('/admin' , function(){ return view('admin.index'); } );
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use Illuminate\Database\Schema\Blueprint;
4+
use Illuminate\Database\Migrations\Migration;
5+
6+
class AddForeigenKeysToPostsTable extends Migration
7+
{
8+
/**
9+
* Run the migrations.
10+
*
11+
* @return void
12+
*/
13+
public function up()
14+
{
15+
Schema::table('posts', function (Blueprint $table) {
16+
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
17+
});
18+
}
19+
20+
/**
21+
* Reverse the migrations.
22+
*
23+
* @return void
24+
*/
25+
public function down()
26+
{
27+
Schema::table('posts', function (Blueprint $table) {
28+
//
29+
});
30+
}
31+
}
-22.4 KB
Binary file not shown.
-61.5 KB
Binary file not shown.
13.3 KB
Loading
84.8 KB
Loading
84.8 KB
Loading

0 commit comments

Comments
 (0)