1
0
Fork 0
mirror of https://github.com/archtechx/laravel-tips.git synced 2025-12-12 05:14:04 +00:00

Weekly Thread command

This commit is contained in:
Samuel Štancl 2021-10-17 21:36:41 +02:00
parent faefe1c45b
commit 3fe86bed08
3 changed files with 80 additions and 5 deletions

View file

@ -40,12 +40,15 @@ class Thread extends Model
public static function booted()
{
static::creating(fn (self $model) => $model->created_at ??= now());
static::creating(static function (self $model) {
$model->created_at ??= now();
$model->links ??= [];
});
}
public function tips(): HasMany
{
return $this->hasMany(Tip::class);
return $this->hasMany(Tip::class, 'thread_slug');
}
public function author(): BelongsTo

View file

@ -41,7 +41,7 @@ class Tip extends Model
$table->string('slug')->unique();
$table->string('title');
$table->string('tweet_id');
$table->foreignId('thread_slug')->nullable();
$table->string('thread_slug')->nullable();
$table->foreignId('author_username')->constrained('authors', 'username');
$table->json('images')->default('[]');
$table->timestamp('created_at');
@ -87,9 +87,9 @@ class Tip extends Model
public static function booted()
{
static::creating(function (self $model) {
static::creating(static function (self $model) {
$model->created_at ??= now();
$model->slug ??= $this->defaultSlug();
$model->slug ??= $model->defaultSlug();
});
static::addGlobalScope('order', fn (Builder $query) => $query->orderBy('created_at', 'desc'));