progress save

This commit is contained in:
Dym Sohin 2024-08-11 11:53:49 +02:00
parent 9c430b9593
commit 12f67c4e4e
11 changed files with 197 additions and 15 deletions

View File

@ -9,6 +9,7 @@ use Illuminate\Support\Arr;
class Article extends Model
{
protected $table = 'articles';
protected $fillable = [ 'title', 'slug', 'body' ];
use HasFactory;
@ -23,4 +24,22 @@ class Article extends Model
return $article;
}
public static function findById(string $id): array
{
$article = Arr::first(Article::all(), fn($article) => $article['id'] === $id);
if(! $article ) {
abort(404);
}
return $article;
}
public function user_id()
{
return $this->belongsTo(User::class);
}
}

11
app/Models/Tag.php Normal file
View File

@ -0,0 +1,11 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Tag extends Model
{
use HasFactory;
}

View File

@ -44,4 +44,9 @@ class User extends Authenticatable
'password' => 'hashed',
];
}
public function articles()
{
return $this->hasMany(Article::class);
}
}

View File

@ -0,0 +1,23 @@
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Tag>
*/
class TagFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
//
];
}
}

View File

@ -13,16 +13,17 @@ return new class extends Migration
{
Schema::create('articles', function (Blueprint $table) {
$table->id();
$table->string('slug');
$table->string('slug')->unique();
$table->string('title');
$table->text('body');
$table->integer('category');
$table->integer('user_id')->references('id')->on('users');
$table->integer('category')->references('id')->on('categories');
$table->boolean('is_collection');
$table->integer('tag_1');
$table->integer('tag_2');
$table->integer('tag_3');
$table->integer('tag_4');
$table->integer('tag_5');
$table->integer('tag_1')->references('id')->on('tags');
$table->integer('tag_2')->references('id')->on('tags');
$table->integer('tag_3')->references('id')->on('tags');
$table->integer('tag_4')->references('id')->on('tags');
$table->integer('tag_5')->references('id')->on('tags');
$table->timestamps();
});
}

View File

@ -0,0 +1,30 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('color');
$table->integer('order_id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('categories');
}
};

View File

@ -0,0 +1,35 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('tags', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('color');
$table->timestamps();
});
Schema::create('article_tag', function (Blueprint $table) {
$table->id();
$table->foreignIdFor(\App\Models\Article::class, 'articles');
$table->foreignIdFor(\App\Models\Tag::class);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('tags');
}
};

View File

@ -575,6 +575,7 @@ ul > li::before
; padding : 0.5rem
; border-radius : 0.5rem
; justify-content : center
; align-items : center
; min-width: 16rem
}

View File

@ -4,6 +4,17 @@
{{ $attributes->merge() }}
>
@elseif( 'checkbox' == $attributes['type'] )
<div class="x-input"
title="{{ $attributes['title'] ?? $slot }}"
>
<input type="checkbox" name="{{ $slug }}"
{{ $attributes['value'] ? 'checked="checked"' : '' }}
>
<label for={{ $slug }}></label>
</div>
@elseif( 'textarea' == $attributes['type'] )
<div class="x-input"

View File

@ -1,15 +1,33 @@
<x-layout-dashboard>
<form class="col c w-30" method="post" name="edit_form" action="/edit">
<form class="col c w-30" method="post" action="/edit">
@csrf
<x-input name="id" type="hidden"
value="{{ $id ?? 0 }}">
<x-input name="title" class="w-30"
value="{{ $title ?? '' }}">
<x-input name="slug" class="w-30" pattern="[A-Za-z0-9-]+"
<x-input type="hidden" value="{{ $id ?? 0 }}">id</x-input>
<x-input class="w-30" value="{{ $title ?? '' }}">title</x-input>
<x-input class="w-30" pattern="[A-Za-z0-9-]+"
value="{{ $slug ?? '' }}"
required>
<textarea name="body" class="w-30">{{ $body ?? '' }}</textarea>
required>slug</x-input>
<x-input type="textarea" class="w-30"
value="{{ $body ?? '' }}"
>body</x-input>
<x-input class="w-30" pattern="[A-Za-z0-9-]+"
value="{{ $category ?? '' }}">category</x-input>
<x-input type="checkbox" pattern="[A-Za-z0-9-]+"
value="{{ $is_collection ?? '' }}">is_collection</x-input>
<x-input class="w-30" pattern="[A-Za-z0-9-]+"
value="{{ $tag_1 ?? '' }}">tag_1</x-input>
<x-input class="w-30" pattern="[A-Za-z0-9-]+"
value="{{ $tag_2 ?? '' }}">tag_2</x-input>
<x-input class="w-30" pattern="[A-Za-z0-9-]+"
value="{{ $tag_3 ?? '' }}">tag_3</x-input>
<x-input class="w-30" pattern="[A-Za-z0-9-]+"
value="{{ $tag_4 ?? '' }}">tag_4</x-input>
<x-input class="w-30" pattern="[A-Za-z0-9-]+"
value="{{ $tag_5 ?? '' }}">tag_5</x-input>
<button type="submit">Save</button>
</form>

View File

@ -24,6 +24,21 @@ Route::view('/notes', 'notes');
Route::get('/new', function () {
return view('new');
});
Route::post('/new', function (string $title = '', string $slug = '', string $body = '') {
$article = Article::create(
[ 'title' => $title
, 'slug' => $slug
, 'body' => $body
]);
return view('edit',
[ 'article' => $article
, 'title' => $article['title']
, 'slug' => $article['slug']
, 'body' => $article['body']
]);
});
Route::get('/edit/{slug}', function (string $slug) {
$article = Article::find($slug);
@ -36,6 +51,19 @@ Route::get('/edit/{slug}', function (string $slug) {
]);
})->where('slug', '[A-Za-z0-9-]+');
Route::post('/edit/{id}', function (string $id) {
$article = Article::findById($id);
return view('edit',
[ 'article' => $article
, 'title' => $article['title']
, 'slug' => $article['slug']
, 'body' => $article['body']
]);
})->where('id', '[0-9-]+');
Route::get('/articles', function () {
return view('articles', [
'articles' => Article::all(),