Skip to content

Commit 46081d7

Browse files
author
RK M
committed
updated
1 parent e7d1e32 commit 46081d7

18 files changed

+1546
-1
lines changed

README.md

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,72 @@
1-
# bootstrap-laravel-crud-generator
1+
![Laravel Crud Generator](https://banners.beyondco.de/Laravel%20CRUD.png?theme=dark&packageManager=composer+require&packageName=ibex%2Fcrud-generator&pattern=architect&style=style_1&description=Laravel+CRUD+Generator&md=1&showWatermark=0&fontSize=100px&images=gift)
2+
3+
4+
![Packagist](https://img.shields.io/badge/Packagist-v1.3.2-green.svg?style=flat-square)
5+
![Licence](https://img.shields.io/badge/Licence-MIT-green.svg?style=flat-square)
6+
![StyleCI](https://img.shields.io/badge/StyleCI-pass-green.svg?style=flat-square)
7+
8+
9+
This Laravel Generator package provides and generate Controller, Model (with eloquent relations) and Views in **Bootstrap** for your development of your applications with single command.
10+
11+
- Will create **Model** with Eloquent relations
12+
- Will create **Controller** with all resources
13+
- Will create **views** in Bootstrap
14+
15+
## Requirements
16+
Laravel >= 5.5
17+
PHP >= 7.1
18+
19+
## Installation
20+
1 - Install
21+
```
22+
composer require Cooltriks/bootstrap-laravel-crud-generator --dev
23+
```
24+
2- Publish the default package's config
25+
```
26+
php artisan vendor:publish --tag=crud
27+
```
28+
29+
## Usage
30+
```
31+
php artisan make:crud {table_name}
32+
33+
php artisan make:crud banks
34+
```
35+
36+
Add a route in `web.php`
37+
```
38+
Route::resource('banks', 'BankController');
39+
```
40+
Route name in plural slug case.
41+
42+
#### Options
43+
- Custom Route
44+
```
45+
php artisan make:crud {table_name} --route={route_name}
46+
```
47+
48+
## Example
49+
50+
*Model*
51+
![Model](https://i.imgur.com/zTSoYvJ.png)
52+
53+
54+
*Controller*
55+
![Controller](https://i.imgur.com/G1ytmcL.png)
56+
57+
58+
*Listing*
59+
![Listing](https://i.imgur.com/UH5XGuw.png)
60+
61+
62+
*Form*
63+
![Form](https://i.imgur.com/poRiZRO.png)
64+
65+
66+
## Author
67+
68+
M Awais // [Email Me](mailto:[email protected])
69+
70+
[Buy me a Coffee](https://ko-fi.com/C0C8VT1M)
71+
72+
[![ko-fi](https://www.ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/C0C8VT1M)

composer.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "Cooltriks/bootstrap-laravel-crud-generator",
3+
"description": "Laravel Crud Generator based on https://github.com/takielias/coolhax-crud-generator",
4+
"license": "MIT",
5+
"authors": [
6+
{
7+
"name": "Rohit Kumar",
8+
"email": "[email protected]"
9+
}
10+
],
11+
"keywords": [
12+
"laravel",
13+
"bootstrap",
14+
"crud",
15+
"crud generator",
16+
"laravel crud generator",
17+
"laravel package"
18+
],
19+
"require": {
20+
"laravel/framework": "^9.0|^10.0",
21+
"laravelcollective/html": "^6.0"
22+
},
23+
"autoload": {
24+
"psr-4": {
25+
"Coolhax\\CoolhaxCrudGenerator\\": "src/"
26+
}
27+
},
28+
"extra": {
29+
"laravel": {
30+
"providers": [
31+
"Coolhax\\CoolhaxCrudGenerator\\CoolhaxCrudServiceProvider"
32+
]
33+
}
34+
},
35+
"minimum-stability": "dev",
36+
"prefer-stable": true
37+
}

src/Commands/CoolhaxCrudGenerator.php

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
<?php
2+
3+
namespace Coolhax\CoolhaxCrudGenerator\Commands;
4+
5+
use Illuminate\Support\Str;
6+
7+
/**
8+
* Class CoolhaxCrudGenerator.
9+
*
10+
* @author Awais <[email protected]>
11+
*/
12+
class CoolhaxCrudGenerator extends GeneratorCommand
13+
{
14+
/**
15+
* The name and signature of the console command.
16+
*
17+
* @var string
18+
*/
19+
protected $signature = 'make:crud
20+
{name : Table name}
21+
{--route= : Custom route name}';
22+
23+
/**
24+
* The console command description.
25+
*
26+
* @var string
27+
*/
28+
protected $description = 'Create bootstrap CRUD operations';
29+
30+
/**
31+
* Execute the console command.
32+
*
33+
* @return bool|null
34+
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
35+
*
36+
*/
37+
public function handle()
38+
{
39+
$this->info('Running Crud Generator ...');
40+
41+
$this->table = $this->getNameInput();
42+
43+
// If table not exist in DB return
44+
if (!$this->tableExists()) {
45+
$this->error("`{$this->table}` table not exist");
46+
47+
return false;
48+
}
49+
50+
// Build the class name from table name
51+
$this->name = $this->_buildClassName();
52+
53+
// Generate the crud
54+
$this->buildOptions()
55+
->buildController()
56+
->buildModel()
57+
->buildViews();
58+
59+
$this->info('Created Successfully.');
60+
61+
return true;
62+
}
63+
64+
/**
65+
* Build the Controller Class and save in app/Http/Controllers.
66+
*
67+
* @return $this
68+
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
69+
*
70+
*/
71+
protected function buildController()
72+
{
73+
$controllerPath = $this->_getControllerPath($this->name);
74+
75+
if ($this->files->exists($controllerPath) && $this->ask('Already exist Controller. Do you want overwrite (y/n)?', 'y') == 'n') {
76+
return $this;
77+
}
78+
79+
$this->info('Creating Controller ...');
80+
81+
$replace = $this->buildReplacements();
82+
83+
$controllerTemplate = str_replace(
84+
array_keys($replace), array_values($replace), $this->getStub('Controller')
85+
);
86+
87+
$this->write($controllerPath, $controllerTemplate);
88+
89+
return $this;
90+
}
91+
92+
/**
93+
* @return $this
94+
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
95+
*
96+
*/
97+
protected function buildModel()
98+
{
99+
$modelPath = $this->_getModelPath($this->name);
100+
101+
if ($this->files->exists($modelPath) && $this->ask('Already exist Model. Do you want overwrite (y/n)?', 'y') == 'n') {
102+
return $this;
103+
}
104+
105+
$this->info('Creating Model ...');
106+
107+
// Make the models attributes and replacement
108+
$replace = array_merge($this->buildReplacements(), $this->modelReplacements());
109+
110+
$modelTemplate = str_replace(
111+
array_keys($replace), array_values($replace), $this->getStub('Model')
112+
);
113+
114+
$this->write($modelPath, $modelTemplate);
115+
116+
return $this;
117+
}
118+
119+
/**
120+
* @return $this
121+
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
122+
*
123+
* @throws \Exception
124+
*/
125+
protected function buildViews()
126+
{
127+
$this->info('Creating Views ...');
128+
129+
$tableHead = "\n";
130+
$tableBody = "\n";
131+
$viewRows = "\n";
132+
$form = "\n";
133+
134+
foreach ($this->getFilteredColumns() as $column) {
135+
$title = Str::title(str_replace('_', ' ', $column));
136+
137+
$tableHead .= $this->getHead($title);
138+
$tableBody .= $this->getBody($column);
139+
$viewRows .= $this->getField($title, $column, 'view-field');
140+
$form .= $this->getField($title, $column, 'form-field');
141+
}
142+
143+
$replace = array_merge($this->buildReplacements(), [
144+
'{{tableHeader}}' => $tableHead,
145+
'{{tableBody}}' => $tableBody,
146+
'{{viewRows}}' => $viewRows,
147+
'{{form}}' => $form,
148+
]);
149+
150+
$this->buildLayout();
151+
152+
foreach (['index', 'create', 'edit', 'form', 'show'] as $view) {
153+
$viewTemplate = str_replace(
154+
array_keys($replace), array_values($replace), $this->getStub("views/{$view}")
155+
);
156+
157+
$this->write($this->_getViewPath($view), $viewTemplate);
158+
}
159+
160+
return $this;
161+
}
162+
163+
/**
164+
* Make the class name from table name.
165+
*
166+
* @return string
167+
*/
168+
private function _buildClassName()
169+
{
170+
return Str::studly(Str::singular($this->table));
171+
}
172+
}

0 commit comments

Comments
 (0)