File tree Expand file tree Collapse file tree 3 files changed +106
-0
lines changed Expand file tree Collapse file tree 3 files changed +106
-0
lines changed Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace App \Models ;
4
+
5
+ use Illuminate \Contracts \Database \Eloquent \Builder ;
6
+ use Illuminate \Database \Eloquent \Factories \HasFactory ;
7
+ use Illuminate \Database \Eloquent \Model ;
8
+ use Illuminate \Database \Eloquent \Relations \BelongsTo ;
9
+
10
+ class Article extends Model
11
+ {
12
+ use HasFactory;
13
+
14
+ protected $ casts = [
15
+ 'published_at ' => 'datetime ' ,
16
+ ];
17
+
18
+ public function scopePublished (Builder $ query ): void
19
+ {
20
+ $ query ->whereDate ('published_at ' , '<= ' , now ());
21
+ }
22
+
23
+ public function author (): BelongsTo
24
+ {
25
+ return $ this ->belongsTo (User::class, 'author_id ' );
26
+ }
27
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Database \Factories ;
4
+
5
+ use App \Models \Article ;
6
+ use App \Models \User ;
7
+ use Illuminate \Database \Eloquent \Factories \Factory ;
8
+
9
+ /**
10
+ * @extends Factory<Article>
11
+ */
12
+ class ArticleFactory extends Factory
13
+ {
14
+ public function definition (): array
15
+ {
16
+ return [
17
+ 'author_id ' => User::factory (),
18
+ 'slug ' => fake ()->unique ()->slug (3 ),
19
+ 'title ' => fake ()->sentence (),
20
+ 'excerpt ' => fake ()->paragraph (1 , false ),
21
+ 'content ' => implode (PHP_EOL .PHP_EOL , fake ()->paragraphs ()),
22
+ ];
23
+ }
24
+
25
+ public function published (): static
26
+ {
27
+ return $ this ->state (fn () => [
28
+ 'published_at ' => now (),
29
+ ]);
30
+ }
31
+
32
+ public function scheduled (): static
33
+ {
34
+ return $ this ->state (fn () => [
35
+ 'published_at ' => now ()->addMinute (),
36
+ ]);
37
+ }
38
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ use Illuminate \Database \Migrations \Migration ;
4
+ use Illuminate \Database \Schema \Blueprint ;
5
+ use Illuminate \Support \Facades \Schema ;
6
+
7
+ return new class extends Migration
8
+ {
9
+ /**
10
+ * Run the migrations.
11
+ */
12
+ public function up (): void
13
+ {
14
+ Schema::create ('articles ' , function (Blueprint $ table ) {
15
+ $ table ->id ();
16
+
17
+ $ table ->unsignedBigInteger ('author_id ' )
18
+ ->nullable ();
19
+
20
+ $ table ->foreign ('author_id ' )
21
+ ->references ('id ' )->on ('users ' )
22
+ ->nullOnDelete ();
23
+
24
+ $ table ->string ('slug ' )->unique ();
25
+ $ table ->string ('title ' , 255 );
26
+ $ table ->string ('excerpt ' , 400 );
27
+ $ table ->text ('content ' );
28
+
29
+ $ table ->timestamp ('published_at ' )->default (now ());
30
+ $ table ->timestamps ();
31
+ });
32
+ }
33
+
34
+ /**
35
+ * Reverse the migrations.
36
+ */
37
+ public function down (): void
38
+ {
39
+ Schema::dropIfExists ('articles ' );
40
+ }
41
+ };
You can’t perform that action at this time.
0 commit comments