8000 posts · cba85/laravel8-saas@2cde162 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Jul 6, 2023. It is now read-only.

Commit 2cde162

Browse files
committed
posts
1 parent e8656e8 commit 2cde162

File tree

19 files changed

+153
-8
lines changed

19 files changed

+153
-8
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,19 @@ Add your database and Stripe credentials inside the `.env` file.
2020
$ php artisan migrate
2121
```
2222

23+
```bash
24+
$ php artisan db:seed # adds 10 fake users and 10 fake posts
25+
```
26+
2327
Manually add your Stripe products in `plans` table, including Stripe Id.
2428

2529
## Usage
2630

2731
```bash
2832
$ php artisan serve
2933
```
34+
35+
## Dependencies
36+
37+
- [Laravel UI](https://github.com/laravel/ui) (Bootstrap 4)
38+
- [Laravel Cashier 12](https://laravel.com/docs/8.x/billing)

app/Http/Controllers/PostController.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,40 @@
22

33
namespace App\Http\Controllers;
44

5+
use App\Models\Post;
56
use Illuminate\Http\Request;
67

78
class PostController extends Controller
89
{
910
public function index()
1011
{
11-
return view('posts.index');
12+
$posts = Post::orderBy('id', 'DESC')->get();
13+
14+
return view('posts.index', compact('posts'));
1215
}
1316

14-
public function store()
17+
public function create()
1518
{
1619
return view('posts.create');
1720
}
21+
22+
public function store(Request $request)
23+
{
24+
$request->validate([
25+
'title' => 'required',
26+
'body' => 'required',
27+
'img' => 'required'
28+
]);
29+
30+
$path = $request->img->store('storage');
31+
32+
Post::create([
33+
'title' => $request->title,
34+
'body' => $request->body,
35+
'img' => $path,
36+
'user_id' => $request->user()->id
37+
]);
38+
39+
return redirect()->route('posts.index');
40+
}
1841
}

app/Models/Post.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ class Post extends Model
99
{
1010
use HasFactory;
1111

12+
protected $fillable = ['title', 'body', 'img', 'user_id'];
13+
1214
public function user()
1315
{
1416
$this->belongsTo(User::class);

config/filesystems.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
*/
6868

6969
'links' => [
70-
public_path('storage') => storage_path('app/public'),
70+
public_path('storage') => storage_path('app/public')
7171
],
7272

7373
];

database/factories/PostFactory.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Database\Factories;
4+
5+
use App\Models\Post;
6+
use Illuminate\Database\Eloquent\Factories\Factory;
7+
8+
class PostFactory extends Factory
9+
{
10+
/**
11+
* The name of the factory's corresponding model.
12+
*
13+
* @var string
14+
*/
15+
protected $model = Post::class;
16+
17+
/**
18+
* Define the model's default state.
19+
*
20+
* @return array
21+
*/
22+
public function definition()
23+
{
24+
return [
25+
'title' => $this->faker->sentence(),
26+
'body' => $this->faker->paragraph(5),
27+
'img' => $this->faker->imageUrl(),
28+
'user_id' => 1
29+
];
30+
}
31+
}

database/seeders/DatabaseSeeder.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class DatabaseSeeder extends Seeder
1313
*/
1414
public function run()
1515
{
16-
// \App\Models\User::factory(10)->create();
16+
\App\Models\User::factory(10)->create();
17+
\App\Models\Post::factory(10)->create();
1718
}
1819
}

resources/views/layouts/app.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
<title>{{ config('app.name', 'Laravel') }}</title>
1111

1212
<!-- Scripts -->
13-
<script src="{{ secure_asset('js/app.js') }}" defer></script>
13+
<script src="/js/app.js" defer></script>
1414

1515
<!-- Fonts -->
1616
<link rel="dns-prefetch" href="//fonts.gstatic.com">
1717
<link href="https://fonts.googleapis.com/css?family=Nunito" rel="stylesheet">
1818

1919
<!-- Styles -->
20-
<link href="{{ secure_asset('css/app.css') }}" rel="stylesheet">
20+
<link href="/css/app.css" rel="stylesheet">
2121

2222
@yield('head')
2323
</head>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
@extends('layouts.app')
2+
3+
@section('content')
4+
<div class="container">
5+
<div class="row justify-content-center">
6+
<div class="col-sm-8">
7+
<h1 class="text-center mb-4">Create a new article</h1>
8+
9+
@if ($errors->any())
10+
<div class="alert alert-danger">
11+
<ul>
12+
@foreach ($errors->all() as $error)
13+
<li>{{ $error }}</li>
14+
@endforeach
15+
</ul>
16+
</div>
17+
@endif
18+
19+
<form method="post" action="{{ route('posts.store') }}" enctype="multipart/form-data">
20+
@csrf
21+
<div class="form-group">
22+
<label for="title">Title</label>
23+
<input type="text" class="form-control" id="title" name="title" value="{{ old('title') }}">
24+
</div>
25+
26+
<div class="form-group">
27+
<label for="body">Body</label>
28+
<textarea class="form-control" id="body" name="body" rows="5">{{ old('body') }}</textarea>
29+
</div>
30+
31+
<div class="form-group">
32+
<label for="image">Image</label>
33+
<input type="file" class="form-control-file" id="image" name="img">
34+
</div>
35+
36+
<button type="submit" class="btn btn-primary btn-lg mt-4">Create</button>
37+
</form>
38+
39+
</div>
40+
</div>
41+
</div>
42+
@endsection

resources/views/posts/index.blade.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
@extends('layouts.app')
2+
3+
@section('content')
4+
<div class="container">
5+
<div class="row justify-content-center">
6+
<div class="col-sm-8">
7+
<h1 class="text-center mb-4">Articles</h1>
8+
9+
<div class="text-center mb-5">
10+
<a href="{{ route('posts.create') }}" class="btn btn-link btn-lg">Create a new article</a>
11+
</div>
12+
13+
@if ($posts->count())
14+
15+
@foreach ($posts as $post)
16+
<div class="row no-gutters border rounded overflow-hidden flex-md-row mb-4 shadow-sm h-md-250 position-relative">
17+
<div class="col-sm-6 p-4">
18+
<h3 class="mb-0">{{ $post->title }}</h3>
19+
<div class="mb-1 text-muted">{{ $post->created_at->diffForHumans() }}</div>
20+
<p class="card-text mb-auto">{{ $post->body }}</p>
21+
<a href="#" class="stretched-link">Continue reading</a>
22+
</div>
23+
<img class="col-sm-6" src="{{ asset($post->img) }}" alt="{{ $post->title }}">
24+
</div>
25+
@endforeach
26+
27+
@else
28+
29+
<p class="text-center"><em>No articles yet.</em></p>
30+
31+
@endif
32+
33+
</div>
34+
</div>
35+
</div>
36+
@endsection

routes/web.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
Route::get('/checkout', [App\Http\Controllers\CheckoutController::class, 'index'])->name('checkout');
1616
Route::post('/checkout', [App\Http\Controllers\CheckoutController::class, 'store']);
1717

18-
Route::get('/posts', [App\Http\Controllers\PostController::class, 'index'])->name('posts');
19-
Route::post('/posts', [App\Http\Controllers\PostController::class, 'store']);
18+
Route::get('/posts', [App\Http\Controllers\PostController::class, 'index'])->name('posts.index');
19+
Route::get('/posts/create', [App\Http\Controllers\PostController::class, 'create'])->name('posts.create')->middleware('auth');
20+
Route::post('/posts', [App\Http\Controllers\PostController::class, 'store'])->name('posts.store');
2021

2122
Route::get('/payment/error', function () {
2223
return view('payments.error');

0 commit comments

Comments
 (0)
0