LaraCrud

Laravel Code Generator based on MySQL Database

Text

Tuhin Bepari
digitaldreams40@gmail.com

 

Lets make a model from Database Table.

php artisan laracrud:model posts

 

Here posts is the name of database table.

Please review and write all of the necessary code for your Model. For example define relation, adjust fillable property etc. It will helps a lot when you are generating Request, View files. Because it reads from your model

By default Model name will be the singular of table name. But if you want to rename then pass your model name as second argument. Like php artisan laracrud:model posts BlogPost

Lets make a Policy Class

php artisan laracrud:policy Post

Here Post is the name of the Model we just created

When you already have a controller and model  then you should use pass --controller=YourController. It will read your controller's method and generate policy methods accordingly

Lets register Policy and Model class to AuthServiceProvider

namespace App\Providers;

class AuthServiceProvider extends ServiceProvider
{

  protected $policies = [
        Category::class => CategoryPolicy::class,
        Post::class => PostPolicy::class,
        Tag::class => TagPolicy::class,
       
    ];

// others methods will be here
}

Its time to create Request classes for our post resource

php artisan laracrud:request Post

--resource=all

Here --resource=all means it will generate Index, Show, Create, Store, Edit, Update, Destroy Request classes

namespace App\Http\Requests\Posts;

class Update extends FormRequest
  
 public function authorize()
    {
        return  auth()->user()->can('update', $this->route('post'));
    }

}

php artisan laracrud:request Post --controller=PostController

Here it will read controller public methods and create individual request class for each of the method.

Its time to create a Controller

php artisan laracrud:controller Post

Here Post is the name of the Model

Text

By default controller name will be ModelName+Controller.  You can rename your controller by pasing second argument. Like php artisan laracrud:controller Post MyPostController

 php artisan laracrud:route PostController

Lets generate routes from our newly created PostController

This command will read controller's method signature and register routes based on that. For example, post controller may have  approve method along with resourceful methods.

public function approve(Request $request, Post $post, $status){}
Route::resource('posts','PostController');
Route::group(['prefix'=>'posts'],function(){
   Route::get('approve/{status}','PostController')->name('posts.approve');
})

Text

This will generate routes like below

Its time to generate view files.

php artisan laracrud:view Post

--controller=PostController

Resourceful routes name on blade files are generated with table name like posts.index etc. But if we pass --controller as options then it will get correct route name for your links and forms action. Moreover if you have a custom routes which needs a blade file then it will generate too.

You can pass --page= options value can be index, show, create, edit, form, table, modal panel.  This will create one specific page instead of all.

Congratulations

 You just created a crud for your posts resource

Want to see some advanced stuff then stay with us for few minutes.

Package Development

php artisan laracrud:package Blog

It create packages/blog folder. It a barebone Laravel Package folder.

Register parent namespace Blog to your composer.json file.

    "autoload": {
          "psr-4": {
              "App\\": "app/",
              "Blog\\": "packages/blog/src/"
    }

Add your BlogServiceProvider to providers array

 'providers' => [
        \Blog\BlogServiceProvider::class,
]

run composer dump-autoload

Package configuration

By default all of your classes will be stored on inside app folders. But we can changes its destination to newly created package folder.

// open config/laracrud.php file and update following keys

'rootNamespace' => 'Blog',
 'view' => [
    'path' => base_path('packages/blog/resources/views'),
    'namespace' => 'blog'
    
 ],
'route'=>[
      'web' => 'packages/blog/routes/web.php',
      'api' => 'packages/blog/routes/api.php',

      ]
}

Root Namespace is really important. Because all of our Models, Requests, Controllers etc are stored under this. For example Blog is register to 'packages/blog/src/' folder.  So php artisan laracrud:model posts will saved to packages/blog/src/Models instead of app/Models folder.  Same is true for view files. Also view namespace are important too. Because its tells laravel where to find your view files. For example blog::posts.index will tells laravel find to packages/blog/resources/views/posts/index.

Reverse Mygration

php artisan laracrud:migration posts

Migration usually create database table. Here we are going to reverse. Database table will create a migration file.

Here posts is the name of database table

    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->unsigned();
            $table->string('title')->nullable();
            $table->string('status')->default('pending');
            $table->longText('body')->nullable();
            $table->integer('category_id')->unsigned()->nullable();
            $table->dateTime('published_at')->nullable();
            $table->timestamps();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->foreign('category_id')->references('id')->on('blog_categories')->onDelete('cascade');
        });
    }

This code is generated by above command. Its awesome is not its ?

Customize Code Template

Coding Style differ from developer to developer. So you can control how your code will be generated. Code templates are organized by folder in resources/vendor/laracrud/templates . Go there and change the style. After that your code will be generated by reading these files. Please do not remove or change @@placeHolder@@. This will be replaced by application.

 

LaraCrud

By Tuhin Bepari

LaraCrud

Laravel Code Generator based on Database

  • 2,807