Skip to content

Commit

Permalink
add CRUD for post model, add comments to a post, update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
deva7mad committed Jan 18, 2023
1 parent 708adb5 commit 2a5bee0
Show file tree
Hide file tree
Showing 27 changed files with 555 additions and 428 deletions.
84 changes: 27 additions & 57 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,66 +1,36 @@
<p align="center"><a href="https://laravel.com" target="_blank"><img src="https://raw.githubusercontent.com/laravel/art/master/logo-lockup/5%20SVG/2%20CMYK/1%20Full%20Color/laravel-logolockup-cmyk-red.svg" width="400" alt="Laravel Logo"></a></p>

<p align="center">
<a href="https://github.com/laravel/framework/actions"><img src="https://github.com/laravel/framework/workflows/tests/badge.svg" alt="Build Status"></a>
<a href="https://packagist.org/packages/laravel/framework"><img src="https://img.shields.io/packagist/dt/laravel/framework" alt="Total Downloads"></a>
<a href="https://packagist.org/packages/laravel/framework"><img src="https://img.shields.io/packagist/v/laravel/framework" alt="Latest Stable Version"></a>
<a href="https://packagist.org/packages/laravel/framework"><img src="https://img.shields.io/packagist/l/laravel/framework" alt="License"></a>
</p>
# Simple Laravel 9 Blog App
### Post, Comment and Authentication

## About Laravel
# Installation steps

Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experience to be truly fulfilling. Laravel takes the pain out of development by easing common tasks used in many web projects, such as:
### <i>1. Clone the repository<i>
````
git clone https://github.com/deva7mad/simple-blog.git
````

- [Simple, fast routing engine](https://laravel.com/docs/routing).
- [Powerful dependency injection container](https://laravel.com/docs/container).
- Multiple back-ends for [session](https://laravel.com/docs/session) and [cache](https://laravel.com/docs/cache) storage.
- Expressive, intuitive [database ORM](https://laravel.com/docs/eloquent).
- Database agnostic [schema migrations](https://laravel.com/docs/migrations).
- [Robust background job processing](https://laravel.com/docs/queues).
- [Real-time event broadcasting](https://laravel.com/docs/broadcasting).
### <i>2. Configure the database connection<i>
````
cd simple-blog/
Laravel is accessible, powerful, and provides tools required for large, robust applications.
cp .env.example .env
````
#### <i>Then</i> Add your database config to .env

## Learning Laravel
### <i>3. Running Migration </i>
````
php artisan migrate --seed
````
#### <i>This</i> will see the database with 30 posts and for each post 5 comments each comment associated with a user.

Laravel has the most extensive and thorough [documentation](https://laravel.com/docs) and video tutorial library of all modern web application frameworks, making it a breeze to get started with the framework.
### <i>4. Storage Link </i>
````
php artisan storage:link
````

You may also try the [Laravel Bootcamp](https://bootcamp.laravel.com), where you will be guided through building a modern Laravel application from scratch.
### <i>5. Run the app </i>
````
php artisan serve
````
#### <i>You </i> can access dashboard with any generated user email and password will be (password)

If you don't feel like reading, [Laracasts](https://laracasts.com) can help. Laracasts contains over 2000 video tutorials on a range of topics including Laravel, modern PHP, unit testing, and JavaScript. Boost your skills by digging into our comprehensive video library.

## Laravel Sponsors

We would like to extend our thanks to the following sponsors for funding Laravel development. If you are interested in becoming a sponsor, please visit the Laravel [Patreon page](https://patreon.com/taylorotwell).

### Premium Partners

- **[Vehikl](https://vehikl.com/)**
- **[Tighten Co.](https://tighten.co)**
- **[Kirschbaum Development Group](https://kirschbaumdevelopment.com)**
- **[64 Robots](https://64robots.com)**
- **[Cubet Techno Labs](https://cubettech.com)**
- **[Cyber-Duck](https://cyber-duck.co.uk)**
- **[Many](https://www.many.co.uk)**
- **[Webdock, Fast VPS Hosting](https://www.webdock.io/en)**
- **[DevSquad](https://devsquad.com)**
- **[Curotec](https://www.curotec.com/services/technologies/laravel/)**
- **[OP.GG](https://op.gg)**
- **[WebReinvent](https://webreinvent.com/?utm_source=laravel&utm_medium=github&utm_campaign=patreon-sponsors)**
- **[Lendio](https://lendio.com)**

## Contributing

Thank you for considering contributing to the Laravel framework! The contribution guide can be found in the [Laravel documentation](https://laravel.com/docs/contributions).

## Code of Conduct

In order to ensure that the Laravel community is welcoming to all, please review and abide by the [Code of Conduct](https://laravel.com/docs/contributions#code-of-conduct).

## Security Vulnerabilities

If you discover a security vulnerability within Laravel, please send an e-mail to Taylor Otwell via [taylor@laravel.com](mailto:taylor@laravel.com). All security vulnerabilities will be promptly addressed.

## License

The Laravel framework is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).
81 changes: 17 additions & 64 deletions app/Http/Controllers/CommentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,85 +2,38 @@

namespace App\Http\Controllers;

use App\Http\Requests\StoreCommentRequest;
use App\Http\Requests\UpdateCommentRequest;
use App\Models\Post;
use App\Models\Comment;
use Illuminate\Http\RedirectResponse;
use App\Http\Requests\StoreCommentRequest;

class CommentController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}

/**
* Show the form for creating a new resource.
* Create a new controller instance.
*
* @return \Illuminate\Http\Response
* @return void
*/
public function create()
public function __construct()
{
//
$this->middleware('auth');
}

/**
* Store a newly created resource in storage.
*
* @param \App\Http\Requests\StoreCommentRequest $request
* @return \Illuminate\Http\Response
* @param StoreCommentRequest $request
* @param Post $post
* @return RedirectResponse
*/
public function store(StoreCommentRequest $request)
public function store(StoreCommentRequest $request, Post $post): RedirectResponse
{
//
}

/**
* Display the specified resource.
*
* @param \App\Models\Comment $comment
* @return \Illuminate\Http\Response
*/
public function show(Comment $comment)
{
//
}
$data = $request->validated();
$comment = Comment::make($data);
$comment->post()->associate($post);
$comment->user()->associate(auth()->user());
$comment->save();

/**
* Show the form for editing the specified resource.
*
* @param \App\Models\Comment $comment
* @return \Illuminate\Http\Response
*/
public function edit(Comment $comment)
{
//
}

/**
* Update the specified resource in storage.
*
* @param \App\Http\Requests\UpdateCommentRequest $request
* @param \App\Models\Comment $comment
* @return \Illuminate\Http\Response
*/
public function update(UpdateCommentRequest $request, Comment $comment)
{
//
}

/**
* Remove the specified resource from storage.
*
* @param \App\Models\Comment $comment
* @return \Illuminate\Http\Response
*/
public function destroy(Comment $comment)
{
//
return redirect()->back()->with('saved', 'Comment saved.');
}
}
20 changes: 16 additions & 4 deletions app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Post;
use Illuminate\Contracts\Support\Renderable;

class HomeController extends Controller
{
Expand All @@ -13,15 +14,26 @@ class HomeController extends Controller
*/
public function __construct()
{
$this->middleware('auth');
$this->middleware('auth')->except('welcome');
}

/**
* Show the application dashboard.
*
* @return \Illuminate\Contracts\Support\Renderable
* @return Renderable
*/
public function index()
public function welcome(): Renderable
{
$posts = Post::latest()->take(6)->get();
return view('welcome', compact('posts'));
}

/**
* Show the application dashboard.
*
* @return Renderable
*/
public function index(): Renderable
{
return view('home');
}
Expand Down
100 changes: 72 additions & 28 deletions app/Http/Controllers/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,85 +2,129 @@

namespace App\Http\Controllers;

use App\Libs\Uploader;
use App\Models\Post;
use Exception;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Response;
use Illuminate\Contracts\View\View;
use Illuminate\Contracts\View\Factory;
use App\Http\Requests\StorePostRequest;
use App\Http\Requests\UpdatePostRequest;
use App\Models\Post;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Support\Facades\Storage;

class PostController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth')->except('show');
}

/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
* @return Application|Factory|View
*/
public function index()
public function index(): Application|Factory|View
{
//
$posts = Post::latest()->simplePaginate(10);
return view('posts.index', compact('posts'));
}

/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
* @return Application|Factory|View
*/
public function create()
public function create(): Application|Factory|View
{
//
return view('posts.create');
}

/**
* Store a newly created resource in storage.
*
* @param \App\Http\Requests\StorePostRequest $request
* @return \Illuminate\Http\Response
* @param StorePostRequest $request
* @return RedirectResponse
* @throws Exception
*/
public function store(StorePostRequest $request)
public function store(StorePostRequest $request): RedirectResponse
{
//
$data = $request->validated();
$data['image'] = Uploader::upload($data['image']);
$post = Post::make($data);
$post->save();

return redirect()->route('posts.index')->with('saved', 'New Post Saved');

}

/**
* Display the specified resource.
*
* @param \App\Models\Post $post
* @return \Illuminate\Http\Response
* @param Post $post
* @return Application|Factory|View
*/
public function show(Post $post)
public function show(Post $post): Application|Factory|View
{
//
return view('posts.show', compact('post'));
}

/**
* Show the form for editing the specified resource.
*
* @param \App\Models\Post $post
* @return \Illuminate\Http\Response
* @param Post $post
* @return Application|Factory|View
*/
public function edit(Post $post)
public function edit(Post $post): Application|Factory|View
{
//
return view('posts.edit', compact('post'));
}

/**
* Update the specified resource in storage.
*
* @param \App\Http\Requests\UpdatePostRequest $request
* @param \App\Models\Post $post
* @return \Illuminate\Http\Response
* @param UpdatePostRequest $request
* @param Post $post
* @return RedirectResponse
* @throws Exception
*/
public function update(UpdatePostRequest $request, Post $post)
public function update(UpdatePostRequest $request, Post $post): RedirectResponse
{
//
$data = $request->validated();

if ($request->has('image')) {
Storage::delete(asset('images/'. $post->image));
$data['image'] = Uploader::upload($data['image']);
}

$post->update($data);

return redirect()->route('posts.index')->with('saved', 'Post Updated');

}

/**
* Remove the specified resource from storage.
*
* @param \App\Models\Post $post
* @return \Illuminate\Http\Response
* @param Post $post
* @return RedirectResponse
*/
public function destroy(Post $post)
public function destroy(Post $post): RedirectResponse
{
//
if ($post->image) {
Storage::delete(asset('images/'. $post->image));
}

$post->comments()->delete();
$post->delete();

return redirect()->route('posts.index')->with('saved', 'Post Deleted');
}
}
Loading

0 comments on commit 2a5bee0

Please sign in to comment.