Welcome to Laravel Crud Controller repository.
This package may provide basic crud operations and allows you to customize completly the controller. There are two controller:
- CrudController: The main controller that returns a response of type Illuminate\View\View.
- RestCrudController: This controller extends the main controller but change the response type: it will be a JSON.
Simply install with composer.
composer require danieletulone/laravel-base-crud
Before use the controller, you must create all resources for a new model:
php artisan make:model Pizza -a
The -a flag meangin all: controller, model, migration, seeder and factory.
Edit the generated controller.
- Extends RestCrudController instead of Laravel standard Controller.
- (optional) Set the property $model into controller with the namespace of your model.
<?php
namespace App\Http\Controllers;
use DanieleTulone\BaseCrud\Http\Controllers\CrudController;
class PizzaController extends CrudController
{
//
}
- Define routes, for example, in
routes/web.php
.
Route::resource('pizzas', 'PizzaController');
- Create views follow this standard:
|--- resources
|------ views
|--------- pizzas
|------------ index.blade.php
|------------ show.blade.php
- By default the CrudController has store, index, show, update and delete method.
If you want to use create and edit method and views, you must to add the trait HasFrontForms to Controller.
<?php
namespace App\Http\Controllers;
use DanieleTulone\BaseCrud\Http\Controllers\CrudController;
use DanieleTulone\BaseCrud\Traits\HasFrontForms;
class PizzaController extends CrudController
{
use HasFrontForms;
}
and in resources/views/pizzas add:
- create.blade.php
- edit.blade.php
Now, define rules into Pizza model, create the views and migrate table. All crud operations are implemented!
- Follow the basic usage instructions.
- Create a FormRequest and define rules;
php artisan make:request PizzaRequest
- (optional) Set $formRequest property into controller.
The main controller has main operations and default queries. You can edit the full flow of controller and edit queries.
Every method has its query method.
- index : indexQuery
- delete: deleteQuery
- show: showQuery
- create: No method for create
- store: storeQuery
- update: updateQuery
So, in your controller, you can ovveride methods like that:
class PizzaController extends CrudController
{
// ... code
/**
* Query used for index method.
*
* @param mixed $params
* @return mixed
*/
public function indexQuery(&$params)
{
$modelsName = Str::plural($this->getModelName());
$this->params[$modelsName] = $this->model::where('price', '>', 15)->paginate();
}
// ... code
}
Each method follows this flow.
- Get params from post, get or from route url (Ex: /pizza/{pizza} -> in params ypu will have pizza param).
- Execute, if exists, a method called before{METHOD_NAME}. Ex: beforeIndex, beforeStore, etc.
- Validate request, if there is Validable trait.
- Call query method. Ex: indexQuery, storeQuery, etc.
- Execute, if exists, a method called after{METHOD_NAME}. Ex: beforeIndex, beforeStore, etc.
- Return a response (view or json).
So, you can customize operation creating after/before methods in controller.
You can create them manually with or without implements interface.
There are two interface:
- DanieleTulone\BaseCrud\Contracts\AfterActions;
- DanieleTulone\BaseCrud\Contracts\BeforeActions;