Skip to content

Commit 903af83

Browse files
committed
add scope published
1 parent 6d9d5b7 commit 903af83

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

app/Post.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ class Post extends Model
1010
{
1111
use SoftDeletes;
1212

13+
protected $dates = [
14+
'published_at'
15+
];
16+
1317
protected $fillable = [
1418
'title', 'description', 'content', 'image', 'published_at', 'category_id', 'user_id'
1519
];
@@ -49,14 +53,19 @@ public function user()
4953
return $this->belongsTo(User::class);
5054
}
5155

56+
public function scopePublished($query)
57+
{
58+
return $query->where('published_at', '<=', now());
59+
}
60+
5261
public function scopeSearched($query)
5362
{
5463
$search = request()->query('search');
5564

5665
if (!$search) {
57-
return $query;
66+
return $query->published();
5867
}
5968

60-
return $query->where('title', 'LIKE', "%{$search}%");
69+
return $query->published()->where('title', 'LIKE', "%{$search}%");
6170
}
6271
}

resources/views/posts/create.blade.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@
9696
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>
9797
<script>
9898
flatpickr('#published_at', {
99-
enableTime: true
99+
enableTime: true,
100+
enableSeconds: true
100101
})
101102
102103
$(document).ready(function() {

0 commit comments

Comments
 (0)