Skip to content

Commit e9ebc0a

Browse files
authored
Merge pull request #84 from Laravel-Backpack/generating-crud-controllers-with-explicit-fields-and-columns
Generate crud controllers with explicit fields and columns, not setFromDb
2 parents 12a44a2 + feea329 commit e9ebc0a

File tree

2 files changed

+126
-9
lines changed

2 files changed

+126
-9
lines changed

src/Console/Commands/CrudControllerBackpackCommand.php

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Backpack\Generators\Console\Commands;
44

55
use Illuminate\Console\GeneratorCommand;
6+
use Illuminate\Support\Arr;
67
use Illuminate\Support\Str;
78

89
class CrudControllerBackpackCommand extends GeneratorCommand
@@ -89,6 +90,84 @@ protected function replaceNameStrings(&$stub, $name)
8990
return $this;
9091
}
9192

93+
protected function getAttributes($model)
94+
{
95+
$attributes = [];
96+
$model = new $model;
97+
98+
// if fillable was defined, use that as the attributes
99+
if (! count($model->getFillable())) {
100+
$attributes = $model->getFillable();
101+
} else {
102+
// otherwise, if guarded is used, just pick up the columns straight from the bd table
103+
$attributes = \Schema::getColumnListing($model->getTable());
104+
}
105+
106+
return $attributes;
107+
}
108+
109+
/**
110+
* Replace the table name for the given stub.
111+
*
112+
* @param string $stub
113+
* @param string $name
114+
*
115+
* @return string
116+
*/
117+
protected function replaceSetFromDb(&$stub, $name)
118+
{
119+
$class = str_replace($this->getNamespace($name).'\\', '', $name);
120+
$model = 'App\Models\\'.$class;
121+
122+
if (! class_exists($model)) {
123+
return $this;
124+
}
125+
126+
$attributes = $this->getAttributes($model);
127+
128+
// create an array with the needed code for defining fields
129+
$fields = Arr::where($attributes, function ($value, $key) {
130+
return ! in_array($value, ['id', 'created_at', 'updated_at', 'deleted_at']);
131+
});
132+
if (count($fields)) {
133+
foreach ($fields as $key => $field) {
134+
$fields[$key] = "CRUD::field('".$field."');";
135+
}
136+
}
137+
138+
// create an array with the needed code for defining columns
139+
$columns = Arr::where($attributes, function ($value, $key) {
140+
return ! in_array($value, ['id']);
141+
});
142+
if (count($columns)) {
143+
foreach ($columns as $key => $column) {
144+
$columns[$key] = "CRUD::column('".$column."');";
145+
}
146+
}
147+
148+
// replace setFromDb with actual fields and columns
149+
$stub = str_replace('CRUD::setFromDb(); // fields', implode(PHP_EOL.' ', $fields), $stub);
150+
$stub = str_replace('CRUD::setFromDb(); // columns', implode(PHP_EOL.' ', $columns), $stub);
151+
152+
return $this;
153+
}
154+
155+
/**
156+
* Replace the class name for the given stub.
157+
*
158+
* @param string $stub
159+
* @param string $name
160+
*
161+
* @return string
162+
*/
163+
protected function replaceModel(&$stub, $name)
164+
{
165+
$class = str_replace($this->getNamespace($name).'\\', '', $name);
166+
$stub = str_replace(['DummyClass', '{{ class }}', '{{class}}'], $class, $stub);
167+
168+
return $this;
169+
}
170+
92171
/**
93172
* Build the class with the given name.
94173
*
@@ -100,7 +179,12 @@ protected function buildClass($name)
100179
{
101180
$stub = $this->files->get($this->getStub());
102181

103-
return $this->replaceNamespace($stub, $name)->replaceNameStrings($stub, $name)->replaceClass($stub, $name);
182+
$this->replaceNamespace($stub, $name)
183+
->replaceNameStrings($stub, $name)
184+
->replaceModel($stub, $name)
185+
->replaceSetFromDb($stub, $name);
186+
187+
return $stub;
104188
}
105189

106190
/**

src/Console/stubs/crud-controller.stub

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,60 @@ class DummyClassCrudController extends CrudController
1919
use \Backpack\CRUD\app\Http\Controllers\Operations\DeleteOperation;
2020
use \Backpack\CRUD\app\Http\Controllers\Operations\ShowOperation;
2121

22+
/**
23+
* Configure the CrudPanel object. Apply settings to all operations.
24+
*
25+
* @return void
26+
*/
2227
public function setup()
2328
{
24-
$this->crud->setModel('App\Models\DummyClass');
25-
$this->crud->setRoute(config('backpack.base.route_prefix') . '/dummy_class');
26-
$this->crud->setEntityNameStrings('dummy_class', 'DummyTable');
29+
CRUD::setModel(\App\Models\DummyClass::class);
30+
CRUD::setRoute(config('backpack.base.route_prefix') . '/dummy_class');
31+
CRUD::setEntityNameStrings('dummy_class', 'DummyTable');
2732
}
2833

34+
/**
35+
* Define what happens when the List operation is loaded.
36+
*
37+
* @see https://backpackforlaravel.com/docs/crud-operation-list-entries
38+
* @return void
39+
*/
2940
protected function setupListOperation()
3041
{
31-
// TODO: remove setFromDb() and manually define Columns, maybe Filters
32-
$this->crud->setFromDb();
42+
CRUD::setFromDb(); // columns
43+
44+
/**
45+
* Columns can be defined using the fluent syntax or array syntax:
46+
* - CRUD::column('price')->type('number');
47+
* - CRUD::addColumn(['name' => 'price', 'type' => 'number']);
48+
*/
3349
}
3450

51+
/**
52+
* Define what happens when the Create operation is loaded.
53+
*
54+
* @see https://backpackforlaravel.com/docs/crud-operation-create
55+
* @return void
56+
*/
3557
protected function setupCreateOperation()
3658
{
37-
$this->crud->setValidation(DummyClassRequest::class);
59+
CRUD::setValidation(DummyClassRequest::class);
60+
61+
CRUD::setFromDb(); // fields
3862

39-
// TODO: remove setFromDb() and manually define Fields
40-
$this->crud->setFromDb();
63+
/**
64+
* Fields can be defined using the fluent syntax or array syntax:
65+
* - CRUD::field('price')->type('number');
66+
* - CRUD::addField(['name' => 'price', 'type' => 'number']));
67+
*/
4168
}
4269

70+
/**
71+
* Define what happens when the Update operation is loaded.
72+
*
73+
* @see https://backpackforlaravel.com/docs/crud-operation-update
74+
* @return void
75+
*/
4376
protected function setupUpdateOperation()
4477
{
4578
$this->setupCreateOperation();

0 commit comments

Comments
 (0)