Skip to content

Commit 171e767

Browse files
committed
feat (subscriber): add subscriber model and migration
1 parent dbb0519 commit 171e767

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

app/Models/Subscriber.php

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class Subscriber extends Model
8+
{
9+
protected $fillable = [
10+
'email',
11+
'subscribed_at',
12+
'unsubscribed_at',
13+
'unsubscribed_reason',
14+
];
15+
16+
protected $dates = [
17+
'subscribed_at',
18+
'unsubscribed_at',
19+
];
20+
21+
protected static function boot(): void
22+
{
23+
parent::boot();
24+
parent::creating(function ($model) {
25+
$model->subscribed_at = now();
26+
});
27+
}
28+
29+
public function scopeSubscribed($query)
30+
{
31+
return $query->whereNotNull('subscribed_at');
32+
}
33+
34+
public function scopeUnsubscribed($query)
35+
{
36+
return $query->whereNotNull('unsubscribed_at');
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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('subscribers', function (Blueprint $table) {
15+
$table->id();
16+
$table->string('email')->unique();
17+
$table->timestamp('subscribed_at')->nullable();
18+
$table->timestamp('unsubscribed_at')->nullable();
19+
$table->string('unsubscribed_reason')->nullable();
20+
$table->timestamps();
21+
});
22+
}
23+
24+
/**
25+
* Reverse the migrations.
26+
*/
27+
public function down(): void
28+
{
29+
Schema::dropIfExists('subscribers');
30+
}
31+
};

0 commit comments

Comments
 (0)