webvelopers / laravel-crud-generator
Laravel CRUD Generator 是一个库,它实现了一个新的命令来创建:模型、迁移、工厂、种子、请求、控制器(资源)和带有操作测试的文件,还提供了生成完整 API 控制器的附加选项。
Requires
- php: ^8.0|^8.1|^8.2|^8.3
- illuminate/console: ^10.0|^11.0
- illuminate/database: ^10.0|^11.0
- illuminate/support: ^10.0|^11.0
- illuminate/testing: ^10.0|^11.0
Requires (Dev)
- phpunit/phpunit: ^10.0.19
This package is auto-updated.
Last update: 2024-10-01 00:14:47 UTC
README
介绍
Laravel CRUD Generator 是一个库,它实现了一个新的命令来创建:模型、迁移、工厂、种子、请求、控制器(资源)和带有操作测试的文件,还提供了生成完整 API 控制器的附加选项。
许可证
Laravel CRUD Generator 是开源软件,许可协议为 MIT 许可协议。
说明
在 Laravel 应用中,您必须
1. 使用 composer 安装此库
🔳 终端/cmd
composer require webvelopers/laravel-crud-generator
2. 使用一行命令生成所有文件
例如,创建一个新的帖子
🔳 终端/cmd
php artisan crud:generator Post
或者创建一个带 API 控制器的新的帖子
🔳 终端/cmd
php artisan crud:generator Post --api
3. 生成的文件
模型
🗄️ app\Models\Post.php
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Post extends Model { use HasFactory; /** * The table associated with the model. * * @var string */ protected $table = 'posts'; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ // ]; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [ // ]; /** * The attributes that should be cast to native types. * * @var array */ protected $casts = [ // ]; /** * The attributes that aren't mass assignable. * * @var array */ protected $guarded = [ // ]; }
迁移
🗄️ database/migrations/2022_06_25_000000_create_posts_table.php
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreatePostsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('posts', function (Blueprint $table) { $table->id(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('posts'); } }
工厂
🗄️ database/factories/PostFactory.php
<?php namespace Database\Factories; use Illuminate\Database\Eloquent\Factories\Factory; class PostFactory extends Factory { /** * Define the model's default state. * * @return array */ public function definition() { return [ // ]; } }
种子
🗄️ database/seeders/PostSeeder.php
<?php namespace Database\Seeders; use App\Models\Post; use Illuminate\Database\Seeder; class PostSeeder extends Seeder { /** * Run the database seeds. * * @return void */ public function run() { //Post::factory()->count(100)->create(); } }
存储请求
🗄️ app/Http/Requests/PostStoreRequest.php
<?php namespace App\Http\Requests; use Illuminate\Foundation\Http\FormRequest; class PostStoreRequest extends FormRequest { /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { return false; } /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ // ]; } }
更新请求
🗄️ app/Http/Requests/PostUpdateRequest.php
<?php namespace App\Http\Requests; use Illuminate\Foundation\Http\FormRequest; class PostUpdateRequest extends FormRequest { /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { return false; } /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ // ]; } }
控制器
🗄️ app/Http/Controllers/PostController.php
<?php namespace App\Http\Controllers; use App\Http\Controllers\Controller; use App\Http\Requests\PostStoreRequest; use App\Http\Requests\PostUpdateRequest; use App\Models\Post; class PostController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { // } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { // } /** * Store a newly created resource in storage. * * @param \App\Http\Requests\PostStoreRequest $request * @return \Illuminate\Http\Response */ public function store(PostStoreRequest $request) { // } /** * Display the specified resource. * * @param mixed $post * @return \Illuminate\Http\Response */ public function show($post) { // } /** * Show the form for editing the specified resource. * * @param \App\Models\Post $post * @return \Illuminate\Http\Response */ public function edit(Post $post) { // } /** * Update the specified resource in storage. * * @param \App\Http\Requests\PostUpdateRequest $request * @param mixed $post * @return \Illuminate\Http\Response */ public function update(PostUpdateRequest $request, $post) { // } /** * Remove the specified resource from storage. * * @param mixed $post * @return \Illuminate\Http\Response */ public function destroy($post) { // } }
路由
🗄️ routes/web.php
Route::resource('posts', \App\Http\Controllers\PostController::class);
API 控制器
🗄️ app/Http/Controllers/Api/PostController.php
<?php namespace App\Http\Controllers\Api; use App\Http\Controllers\Controller; use App\Http\Requests\PostStoreRequest; use App\Http\Requests\PostUpdateRequest; use App\Models\Post; class PostController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { $posts = Post::all(); return response()->json($posts, 200); } /** * Store a newly created resource in storage. * * @param \App\Http\Requests\PostStoreRequest $request * @return \Illuminate\Http\Response */ public function store(PostStoreRequest $request) { $post = Post::create($request->all()); return response()->json($post, 201); } /** * Display the specified resource. * * @param mixed $post * @return \Illuminate\Http\Response */ public function show($post) { $post = Post::findOrFail($post); return response()->json($post, 200); } /** * Update the specified resource in storage. * * @param \App\Http\Requests\PostUpdateRequest $request * @param mixed $post * @return \Illuminate\Http\Response */ public function update(PostUpdateRequest $request, $post) { $post = Post::findOrFail($post); $post->update($request->all()); return response()->json(null, 204); } /** * Remove the specified resource from storage. * * @param mixed $post * @return \Illuminate\Http\Response */ public function destroy($post) { Post::destroy($post); return response()->json(null, 204); } }
API 路由
🗄️ routes/api.php
Route::apiResource('posts', \App\Http\Controllers\Api\PostController::class);
测试
✍🏻 Tests/Feature/PostTest.php
<?php namespace Tests\Feature; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\WithFaker; use Tests\TestCase; class PostTest extends TestCase { /** * A basic feature test post. */ public function test_post(): void { // } }
4. 简单编辑生成的文件
编辑模型
🗄️ app\Models\Post.php
... /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'title', 'content', ]; ...
编辑迁移
🗄️ database/migrations/2022_06_25_000000_create_posts_table.php
... /** * Run the migrations. * * @return void */ public function up() { Schema::create('posts', function (Blueprint $table) { $table->id(); $table->string('title'); $table->text('content'); $table->timestamps(); }); } ...
编辑工厂
🗄️ database/factories/PostFactory.php
... /** * Define the model's default state. * * @return array */ public function definition() { return [ 'title' => $this->faker->sentence, 'content' => $this->faker->paragraph, ]; } ...
编辑种子
🗄️ database/seeders/PostSeeder.php
/** * Run the database seeds. * * @return void */ public function run() { Post::factory()->count(100)->create(); }
编辑数据库种子
🗄️ database/seeders/DatabaseSeeder.php
/** * Seed the application's database. * * @return void */ public function run() { // \App\Models\User::factory(10)->create(); $this->call(PostSeeder::class); }
编辑存储请求
🗄️ app/Http/Requests/PostStoreRequest.php
... /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ 'title' => 'required|string|max:255', 'content' => 'required|string', ]; } ...
编辑更新请求
🗄️ app/Http/Requests/PostUpdateRequest.php
... /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ 'title' => 'required|string|max:255', 'content' => 'required|string', ]; } ...
5. 执行迁移
php artisan migrate --seed
享受它
made with ♥ by... __ __ \/\/\/\/ \/\/\/ \/\/ www.webvelopers.net