If you're using an IDE like PhpStorm, you could always setup live templates (or equivalents in other editors) to do it for you, but there's no package that I'm aware of.
any CRUD generator in laravel? Im sick of copy the same shit
Hello, Im sick of copy my CRUD methods of Laravel over and over.
do you know some package to generate a CRUD of any model autmathic (or generate some taking my own methods?
or do you know some technique to do that like a Trait in PHP?
I just use my gist (github) to copy and replace the model class (like User::find($id))
Like deringer mentioned create your own live template in phpstorm ;)
You have this package as well for quick Laravel functions from facades ;) https://github.com/koomai/phpstorm-laravel-live-templates
You can inject a model in a controller and set namespace for the views.
class CrudController extends Controller
{
private $model;
public function __construct(MyModelInterface $model)
{
$this->model = $model;
}
public function index($token)
{
$models = $this->models->all();
return view('models::index', ['token' => $token, 'models' => $models]);
}
public function show($token, $id)
{
$model = $this->models->findOrFail($id);
return view('models::show', ['token' => $token, 'model' => $model]);
}
public function create($token)
{
$model = $this->models->newInstance();
return view('models::create', ['token' => $token, 'model' => $model]);
}
public function store(MyModelFormRequestInterface $request, $token)
{
$model = $this->models->newInstance();
$model->fill($request->all());
return redirect()->route('models.show', [$token, $id]);
}
// And so on...
}
Then you have generic routes accepting a model token :
$tokens = config('my_app.model_tokens');
Route::pattern('token', implode('|', $tokens));
Route::get('{token}', ['as' => 'models.index', 'uses' => 'CrudController@index']);
Route::get('{token}/{id}', ['as' => 'models.show', 'uses' => 'CrudController@show']);
// ans so on...
Then you bind the interfaces & view namespace according to the first segment of the url (the token) in the service provider.
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
$this->app->call([$this, 'registerViewNamespace']);
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->call([$this, 'registerToken']);
$this->app->call([$this, 'registerMyModel']);
$this->app->call([$this, 'registerMyModelFormRequest']);
}
/**
* Set the token based on the request url.
*
* @return void
*/
public function registerToken(Request $request)
{
$this->app->singleton('token', function ($app) use($request) {
$token = $request->segment(1);
$tokens = $app['config']['my_app.model_tokens'];
$intersect = array_intersect([$token], $tokens);
return array_shift($intersect);
});
}
/**
* Bind the MyModelInterface to a concrete Model according to the token.
*
* @return void
*/
public function registerMyModel()
{
if(!$token = $this->app['token']) return;
$this->app->bind(MyModelInterface::class, function ($app) use($token) {
return new $app['config']['my_app.model_classes'][$token];
});
}
/**
* Bind the MyModelFormRequestInterface to a concrete FormRequest according to the token.
*
* @return void
*/
public function registerMyModelFormRequest()
{
if(!$token = $this->app['token']) return;
$this->app->bind(MyModelFormRequestInterface::class, function ($app) use($token) {
return new $app['config']['my_app.model_requests'][$token];
});
}
/**
* Register a namespace for the model's views according to the token.
*
* @return void
*/
public function registerViewNamespace(ViewFactory $view)
{
if(!$token = $this->app['token']) return;
$namespace = $this->app['config']['my_app.view_namespaces'][$token];
$view->prependNamespace('models', [base_path($namespace)]);
}
}
And it's done. You can forget about crud controller. Add a model class, a form request, a folder for its views, map everything in your config. Of course your models have to implement a MyModelInterface and your form requests have to implement a MyModelFormRequestInterface.
The coolest thing is if a model has a slightly different logic for a method you can create its own controller extending CrudController and overwriting this particular method.
I manage an app with a lot of crud models like this. I Was sick of copy pasting controllers too.
@pmall excelent! can I see a open source project using this technique to complete understand the Crud Generator??
I like it! :D
This is from me and this is not an open source project :D
But you have all the clues here.
@jhosep412 you can try my CRUD generator which will let you generate everything with a single line command. Try and let me know your feedback. https://github.com/appzcoder/crud-generator
Try https://github.com/engrnvd/laravel-crud-generator . It requires minimum configuration and generates crud based on database fields.
Just give it the table name and the whole working crud code is generated for you.
You only have to give the table name and everything else is taken care of. I hope it's a relief knowing that scalability and simplicity is not destroyed in the process.
Ruby on Rails has CRUD generator for decade. YII framework has CRUD generator for at least 4 years. Symfony 3 has recently added CRUD generator. I can't imagine modern web framework without built in CRUD generator. It's a shame for such popular framework to not have it.
sashah, I couldn't agree more. I just shifted from Yii2 to Laravel 5+ and this is the one thing that is painfully missing in Laravel.
@sashah please feel free to create one - no one is stopping you..
@k00na Why have you replied to a topic that’s been dormant for over a year?
definetly ... https://github.com/appzcoder/crud-generator
I am also faced same problem. Why should i write same logic in Request, Model's what i already write in migrations file. So i create a Laravel Code Generator that will read mysql table structure and generate necessary code for us. https://github.com/digitaldreams
@pmall your solution is great. I am trying to implement it in our CMS. Could you please send example of your config? $app['config']['my_app']
CRUDBooster
Please or to participate in this conversation.