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

homework 01 solution #2

Open
wants to merge 3 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
3 changes: 0 additions & 3 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,3 @@ trim_trailing_whitespace = false

[*.{yml,yaml}]
indent_size = 2

[*.blade.php]
indent_size = 2
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ We would like to extend our thanks to the following sponsors for funding Laravel
- **[Many](https://www.many.co.uk)**
- **[Webdock, Fast VPS Hosting](https://www.webdock.io/en)**
- **[DevSquad](https://devsquad.com)**
- **[Curotec](https://www.curotec.com/)**
- **[Curotec](https://www.curotec.com/services/technologies/laravel/)**
- **[OP.GG](https://op.gg)**

## Contributing
Expand Down
32 changes: 22 additions & 10 deletions app/Http/Controllers/CategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ class CategoryController extends Controller
*/
public function index()
{

$categories = Category::all();
return view('category.index', ['categories' => $categories]);

}

/**
Expand All @@ -25,7 +27,7 @@ public function index()
*/
public function create()
{
// TODO: return category create view
return view("/category.create");
}

/**
Expand All @@ -36,9 +38,14 @@ 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:2|max:30',
'icon' => 'required|url',
]);

$category = Category::create(['name' => $request->name,'icon'=> $request->icon]);

return redirect("/categories");
}

/**
Expand All @@ -49,7 +56,7 @@ public function store(Request $request)
*/
public function show(Category $category)
{
return view('category.show', ['category' => $category]);
return view('category.show' , ['category' => $category]);
}

/**
Expand All @@ -60,7 +67,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 @@ -72,9 +79,14 @@ public function edit(Category $category)
*/
public function update(Request $request, Category $category)
{
// TODO: validate the request
// TODO: update the category using update method
// TODO: return reidrect to categories index
$category = Category::findOrFail($category);

$category = Category::create(['name' => $request->name,'icon'=> $request->icon]);

return redirect("/categories/{$category->id}");



}

/**
Expand All @@ -85,6 +97,6 @@ public function update(Request $request, Category $category)
*/
public function destroy(Category $category)
{
// TODO: look for this
//
}
}
69 changes: 41 additions & 28 deletions app/Http/Controllers/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,63 +3,76 @@
namespace App\Http\Controllers;

use App\Models\Category;
use Illuminate\Http\Request;
use App\Models\Post;
use App\Models\Tag;
use Illuminate\Http\Request;

class PostController extends Controller
{
public function create ()
{
public function show($id) {
$post = Post::findorFail($id);
return view('pages.showpost' , ['post'=>$post]);
}

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

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',
'content' => 'required|min:4',
'title' => 'required|min:4|max:255',
'img' => 'required|url',
'body' => 'required|min:4',
'category_id' => 'required|numeric|exists:categories,id',
'tags' => 'array',
]);

$post = new Post();

]);
$post = new Post;
$post->title = $request->title;
$post->featured_image = $request->featured_image;
$post->content = $request->content;
$post->img = $request->img;
$post->body = $request->body;
$post->category_id = $request->category_id;
$post->save();

$post->tags()->sync($request->tags);

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

return redirect()->route('posts.show', $post);
}
return redirect("/posts/{$post->id}");

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

return view('post.edit', ['post' => $post]);
public function edit($id){
$post = Post::findOrFail($id);
$tags = Tag::all();
return view('post/edit',['post' => $post , 'tags' => $tags]);
}

public function update($id, Request $request)
{
public function update($id, Request $request){

$post = Post::findOrFail($id);

$request->validate([
'title' => 'required|min:4|max:255',
'img' => 'required|url',
'body' => 'required|min:4',
'tags' => 'array',


]);

$post->title = $request->title;
$post->featured_image = $request->featured_image;
$post->content = $request->content;
$post->img = $request->img;
$post->body = $request->body;
$post->save();

$post->tags()->sync($request->tags);


return redirect("/posts/{$post->id}");

}
}
16 changes: 12 additions & 4 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()
{
//
$tags = Tag::all();
return view('tag.index', ['tags' => $tags]);
}

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

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

$tag = Tag::create(['name' => $request->name,'slug'=> $request->slug]);

return redirect("/tags");
}

/**
Expand Down Expand Up @@ -64,7 +72,7 @@ public function edit(Tag $tag)
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Tag $tag
* @param \App\Models\Tags $tags
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Tag $tag)
Expand Down
6 changes: 2 additions & 4 deletions app/Models/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@
class Category extends Model
{
use HasFactory;

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

/**
* Get the posts for the blog post.
*/
public function posts()
{
public function posts(){
return $this->hasMany(Post::class);
}
}
2 changes: 2 additions & 0 deletions app/View/Components/Footer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

class Footer extends Component
{
// public $post;
/**
* Create a new component instance.
*
Expand All @@ -14,6 +15,7 @@ class Footer extends Component
public function __construct()
{
//
// $this->post = $post;
}

/**
Expand Down
28 changes: 28 additions & 0 deletions app/View/Components/container.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 container 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.container');
}
}
28 changes: 28 additions & 0 deletions app/View/Components/layout.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 layout 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.layout');
}
}
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