Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sanabel-blog homework-Asmaa #5

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
36 changes: 25 additions & 11 deletions app/Http/Controllers/CategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class CategoryController extends Controller
*
* @return \Illuminate\Http\Response
*/
public function index()
public function index(Category $category)
{
$categories = Category::all();
return view('category.index', ['categories' => $categories]);
Expand All @@ -25,7 +25,7 @@ public function index()
*/
public function create()
{
// TODO: return category create view
return view('category.create');
}

/**
Expand All @@ -36,9 +36,15 @@ public function create()
*/
public function store(Request $request)
{
// TODO: validate the request
// TODO: make new category using create method
// TODO: return reidrect to categories index
$request->validate([
'name' => 'required|min:4|max:255',
'icon' => 'required|url',
'slug' => 'required|min:4|string'
]);

Category::create($request->all());

return redirect()->route('categories.index')->with('sucess','Created Successfuly');
}

/**
Expand All @@ -60,7 +66,7 @@ public function show(Category $category)
*/
public function edit(Category $category)
{
// TODO: return edit view with $category var
return view('category.edit', ['category' => $category]);
}

/**
Expand All @@ -70,11 +76,17 @@ public function edit(Category $category)
* @param \App\Models\Category $category
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Category $category)
public function update(Category $category, Request $request)
{
// TODO: validate the request
// TODO: update the category using update method
// TODO: return reidrect to categories index
$request->validate([
'name' => 'required|min:4|max:255',
'icon' => 'required|url',
'slug' => 'required|min:4|string'
]);

$category->update($request->all());

return redirect()->route('categories.index', $category)->with('sucess','Updated Successfuly');
}

/**
Expand All @@ -85,6 +97,8 @@ public function update(Request $request, Category $category)
*/
public function destroy(Category $category)
{
// TODO: look for this
$category->delete();

return redirect()->route('categories.index')->with('sucess','Deleted Successfuly');
}
}
24 changes: 19 additions & 5 deletions app/Http/Controllers/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,22 @@

class PostController extends Controller
{
public function create ()
public function create()
{
$categories = Category::all();
$tags = Tag::all();
return view('post.create', ['categories' => $categories, 'tags' => $tags]);
}

public function show ($id) {
public function show($id)
{
$post = Post::findOrFail($id);

return view('post.show', ['post' => $post]);
}

public function store (Request $request) {
public function store(Request $request)
{
$request->validate([
'title' => 'required|min:4|max:255',
'featured_image' => 'required|url',
Expand All @@ -42,7 +44,7 @@ public function store (Request $request) {

// $post = Post::create($request->all());

return redirect()->route('posts.show', $post);
return redirect()->route('posts.show', $post)->with('success', 'Created Successfuly');;
}

public function edit($id)
Expand All @@ -54,12 +56,24 @@ public function edit($id)

public function update($id, Request $request)
{
$request->validate([
'title' => 'required|min:5|max:200',
'featured_image' => 'required|url',
'content' => 'required|min:10',

]);
$post = Post::findOrFail($id);
$post->title = $request->title;
$post->featured_image = $request->featured_image;
$post->content = $request->content;
$post->save();

return redirect("/posts/{$post->id}");
return redirect()->route('posts.update', $post)->with('success', 'edited Successfuly');;
}
public function delete($id)
{
$post = Post::find($id);
$post->delete();
return redirect()->route('home',$post)->with('success', 'Post Deleted');
}
}
35 changes: 28 additions & 7 deletions app/Http/Controllers/TagController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class TagController extends Controller
*/
public function index()
{
//
$tag = Tag::all();
return view('tags.index', ['tags' => $tag]);
}

/**
Expand All @@ -24,7 +25,9 @@ public function index()
*/
public function create()
{
//
return view('tags.create');


}

/**
Expand All @@ -35,7 +38,14 @@ public function create()
*/
public function store(Request $request)
{
//
$request->validate([
'name' => 'required|min:4|max:255',
'slug' => 'required|min:4|string'
]);

Tag::create($request->all());

return redirect()->route('tags.index')->with('sucess','Created Successfuly');
}

/**
Expand All @@ -46,7 +56,8 @@ public function store(Request $request)
*/
public function show(Tag $tag)
{
//
return view('tags.show', ['tags' => $tag]);

}

/**
Expand All @@ -57,7 +68,8 @@ public function show(Tag $tag)
*/
public function edit(Tag $tag)
{
//
return view('tags.edit', ['tags' => $tag]);

}

/**
Expand All @@ -69,7 +81,14 @@ public function edit(Tag $tag)
*/
public function update(Request $request, Tag $tag)
{
//
$request->validate([
'name' => 'required|min:4|max:255',
'slug' => 'required|min:4|string'
]);

$tag->update($request->all());

return redirect()->route('tags.show', $tag)->with('sucess','Updated Successfuly');
}

/**
Expand All @@ -80,6 +99,8 @@ public function update(Request $request, Tag $tag)
*/
public function destroy(Tag $tag)
{
//
$tag->delete();

return redirect()->route('tags.index')->with('sucess','Deleted Successfuly');
}
}
6 changes: 5 additions & 1 deletion app/Models/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Category extends Model
{
use HasFactory;

protected $fillable = ['name', 'icon'];
protected $fillable = ['name', 'icon','slug'];

/**
* Get the posts for the blog post.
Expand All @@ -18,4 +18,8 @@ public function posts()
{
return $this->hasMany(Post::class);
}
public function getRouteKeyName()
{
return 'slug';
}
}
6 changes: 6 additions & 0 deletions app/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
class Post extends Model
{
use HasFactory;
protected $fillable = ['title', 'featured_image','content','slug'];


public function category()
{
Expand All @@ -21,4 +23,8 @@ public function tags()
{
return $this->belongsToMany(Tag::class);
}
public function getRouteKeyName()
{
return 'slug';
}
}
28 changes: 28 additions & 0 deletions app/View/Components/Message.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\View\Components;

use Illuminate\View\Component;

class Message extends Component
{
/**
* Create a new component instance.
*
* @return void
*/
public function __construct()
{
//
}

/**
* Get the view / contents that represent the component.
*
* @return \Illuminate\Contracts\View\View|\Closure|string
*/
public function render()
{
return view('components.message');
}
}
28 changes: 28 additions & 0 deletions app/View/Components/Sidebar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\View\Components;

use Illuminate\View\Component;

class Sidebar extends Component
{
/**
* Create a new component instance.
*
* @return void
*/
public function __construct()
{
//
}

/**
* Get the view / contents that represent the component.
*
* @return \Illuminate\Contracts\View\View|\Closure|string
*/
public function render()
{
return view('components.sidebar');
}
}
Loading