Skip to content
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
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ http://laravelfromscratch.com

## Installation

First clone this repository, install the dependencies, and setup your .env file.
First clone this repository, install the dependencies, and set up your .env file.

```
git clone git@github.com:JeffreyWay/Laravel-From-Scratch-Blog-Project.git blog
composer install
cp .env.example .env
php artisan key:generate
```

Then create the necessary database.
Expand All @@ -27,7 +28,7 @@ php artisan migrate --seed

## Further Ideas

Of course we only had time in the Laravel From Scratch series to review the essentials of a blogging platform. You can certainly take this many
Of course, we only had time in the Laravel From Scratch series to review the essentials of a blogging platform. You can certainly take this many
steps further. Here are some quick ideas that you might play with.

1. Add a `status` column to the posts table to allow for posts that are still in a "draft" state. Only when this status is changed to "published" should they show up in the blog feed.
Expand Down
21 changes: 17 additions & 4 deletions app/Models/Post.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
<?php
<?php /** @noinspection PhpUnused */

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Post extends Model
{
use HasFactory;

protected $with = ['category', 'author'];

public const DEFAULT_POST_THUMBNAIL_URL = 'post/default.jpg';

public function scopeFilter($query, array $filters)
{
$query->when($filters['search'] ?? false, fn($query, $search) =>
Expand All @@ -33,17 +37,26 @@ public function scopeFilter($query, array $filters)
);
}

public function comments()
public function getThumbnailUrl(): string
{
if ($thumbnailPath = $this->getAttribute('thumbnail')) {
return "/storage/$thumbnailPath";
}

return asset('images') . '/' . self::DEFAULT_POST_THUMBNAIL_URL;
}

public function comments(): HasMany
{
return $this->hasMany(Comment::class);
}

public function category()
public function category(): BelongsTo
{
return $this->belongsTo(Category::class);
}

public function author()
public function author(): BelongsTo
{
return $this->belongsTo(User::class, 'user_id');
}
Expand Down
6 changes: 3 additions & 3 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class AppServiceProvider extends ServiceProvider
*
* @return void
*/
public function register()
public function register(): void
{
app()->bind(Newsletter::class, function () {
$client = (new ApiClient)->setConfig([
Expand All @@ -35,12 +35,12 @@ public function register()
*
* @return void
*/
public function boot()
public function boot(): void
{
Model::unguard();

Gate::define('admin', function (User $user) {
return $user->username === 'JeffreyWay';
return $user->email === config('auth.site_admin');
});

Blade::if('admin', function () {
Expand Down
Loading