Skip to content

Commit d94b47f

Browse files
committed
inertia crud posts
1 parent 334144f commit d94b47f

File tree

14 files changed

+519
-497
lines changed

14 files changed

+519
-497
lines changed
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\Http\Requests\Post\StoreRequest;
6+
use App\Http\Requests\Post\UpdateRequest;
7+
use App\Http\Resources\Post\PostResource;
8+
use App\Models\Post;
9+
use Illuminate\Http\Request;
10+
11+
class PostController extends Controller
12+
{
13+
public function index()
14+
{
15+
$posts = Post::all();
16+
$posts = PostResource::collection($posts)->resolve();
17+
return inertia('Post/Index', compact('posts'));
18+
}
19+
public function create()
20+
{
21+
return inertia('Post/Create');
22+
}
23+
public function store(StoreRequest $request)
24+
{
25+
Post::create($request->validated());
26+
return redirect()->route('posts.index');
27+
}
28+
public function show(Post $post)
29+
{
30+
return inertia('Post/Show', compact('post'));
31+
}
32+
public function edit(Post $post)
33+
{
34+
return inertia('Post/Edit', compact('post'));
35+
}
36+
public function update(Post $post, UpdateRequest $request)
37+
{
38+
$post->update($request->validated());
39+
return redirect()->route('posts.index');
40+
}
41+
public function destroy(Post $post)
42+
{
43+
$post->delete();
44+
return redirect()->route('posts.index');
45+
}
46+
}
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace App\Http\Requests\Post;
4+
5+
use Illuminate\Foundation\Http\FormRequest;
6+
7+
class StoreRequest extends FormRequest
8+
{
9+
/**
10+
* Determine if the user is authorized to make this request.
11+
*/
12+
public function authorize(): bool
13+
{
14+
return true;
15+
}
16+
17+
/**
18+
* Get the validation rules that apply to the request.
19+
*
20+
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
21+
*/
22+
public function rules(): array
23+
{
24+
return [
25+
'title' => 'required|string',
26+
'content' => 'required|string',
27+
];
28+
}
29+
}
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace App\Http\Requests\Post;
4+
5+
use Illuminate\Foundation\Http\FormRequest;
6+
7+
class UpdateRequest extends FormRequest
8+
{
9+
/**
10+
* Determine if the user is authorized to make this request.
11+
*/
12+
public function authorize(): bool
13+
{
14+
return true;
15+
}
16+
17+
/**
18+
* Get the validation rules that apply to the request.
19+
*
20+
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
21+
*/
22+
public function rules(): array
23+
{
24+
return [
25+
'title' => 'required|string',
26+
'content' => 'required|string',
27+
];
28+
}
29+
}
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace App\Http\Resources\Post;
4+
5+
use Illuminate\Http\Request;
6+
use Illuminate\Http\Resources\Json\JsonResource;
7+
8+
class PostResource extends JsonResource
9+
{
10+
/**
11+
* Transform the resource into an array.
12+
*
13+
* @return array<string, mixed>
14+
*/
15+
public function toArray(Request $request): array
16+
{
17+
return [
18+
'id' => $this->id,
19+
'title' => $this->title,
20+
'content' => $this->content,
21+
'created_at' => $this->created_at->diffForHumans(),
22+
'updated_at' => $this->updated_at->diffForHumans(),
23+
];
24+
}
25+
}

app/Models/Post.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class Post extends Model
8+
{
9+
protected $guarded = false;
10+
}
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+
use function Laravel\Prompts\table;
8+
9+
return new class extends Migration
10+
{
11+
/**
12+
* Run the migrations.
13+
*/
14+
public function up(): void
15+
{
16+
Schema::create('posts', function (Blueprint $table) {
17+
$table->id();
18+
$table->string('title');
19+
$table->string('content');
20+
$table->timestamps();
21+
});
22+
}
23+
24+
/**
25+
* Reverse the migrations.
26+
*/
27+
public function down(): void
28+
{
29+
Schema::dropIfExists('posts');
30+
}
31+
};

0 commit comments

Comments
 (0)