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

Fix content, add command, update readme

This commit is contained in:
Samuel Štancl 2021-08-27 18:37:33 +02:00
parent 3c3a5fcfd9
commit 69d86147d7
12 changed files with 70 additions and 35 deletions

View file

@ -19,7 +19,7 @@ use Illuminate\Support\Str;
* @property string $content
* @property string $tweet_id
* @property string $author_username
* @property string|null $thread_id
* @property string|null $thread_slug
* @property string[] $images
* @property-read Author $author
* @property-read Thread|null $thread
@ -41,7 +41,7 @@ class Tip extends Model
$table->string('slug')->unique();
$table->string('title');
$table->string('tweet_id');
$table->foreignId('thread_slug')->constrained('threads', 'slug')->nullable();
$table->foreignId('thread_slug')->nullable();
$table->foreignId('author_username')->constrained('authors', 'username');
$table->json('images')->default('[]');
$table->timestamp('created_at');
@ -74,7 +74,7 @@ class Tip extends Model
'title' => (string) Str::of(Str::of($tweet->text)->explode("\n")->first())->rtrim('.')->replaceMatches('/^([^a-zA-Z]*)/', ''), // remove any non-ascii characters from the beginning
'content' => (string) Str::of($tweet->text)->explode("\n")->skip(1)->join("\n"),
'tweet_id' => $tweet->id,
'thread_slug' => $threadSlug ?? Thread::firstWhere('tweet_id', $tweet->threadId)->slug,
'thread_slug' => $threadSlug ?? Thread::firstWhere('tweet_id', $tweet->threadId)?->slug,
'author_username' => Author::firstOrCreate([
'username' => $tweet->author->username,
'name' => $tweet->author->name,
@ -89,7 +89,7 @@ class Tip extends Model
{
static::creating(function (self $model) {
$model->created_at ??= now();
$model->slug ??= Str::slug($model->title);
$model->slug ??= $this->defaultSlug();
});
static::addGlobalScope('order', fn (Builder $query) => $query->orderBy('created_at', 'desc'));
@ -104,4 +104,9 @@ class Tip extends Model
{
return false;
}
public function defaultSlug(): string
{
return Str::slug($this->title);
}
}