Skip to content

Commit

Permalink
Episode 39 complete
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffreyWay committed May 20, 2021
1 parent 3c7f19f commit 599f833
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
5 changes: 5 additions & 0 deletions app/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public function scopeFilter($query, array $filters)
$query
->where('title', 'like', '%' . $search . '%')
->orWhere('body', 'like', '%' . $search . '%'));
$query->when($filters['category'] ?? false, fn($query, $category) =>
$query->whereHas('category', fn ($query) =>
$query->where('slug', $category)
)
);
}

public function category()
Expand Down
7 changes: 0 additions & 7 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@
Route::get('/', [PostController::class, 'index'])->name('home');
Route::get('posts/{post:slug}', [PostController::class, 'show']);

Route::get('categories/{category:slug}', function (Category $category) {
return view('posts', [
'posts' => $category->posts,
'currentCategory' => $category,
'categories' => Category::all()
]);
})->name('category');

Route::get('authors/{author:username}', function (User $author) {
return view('posts', [
Expand Down

1 comment on commit 599f833

@Rice287
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to replace the when() method with if statement, but then I couldn't stack the query with the '&' operator like before. Seems like when() is more than just an alternative of if statement

Please sign in to comment.