Laravel additions and deletions to search

Source: Internet
Author: User

The basic idea is to build a formcontroller, all the controllers that need to be configured to build the backend will inherit this formcontroller. To define a property in Formcontroller:
Class Formcontroller extends Basecontroller {      //corresponding model     protected $model;      All fields are     protected $fields _all;      The list page displays fields     protected $fields _show;      Edit page Display field     protected $fields _edit;      Create a field for the page display     protected $fields _create;}
The model is defined to indicate that the controller is a single-table operation on the model. Defines the Fields_all property to make all the fields a description and definition. This definition and description includes the field display name, whether the field is to be searched, and what the field type is. For list pages, not all properties are displayed, so define aFIELDSSHOW,This number Group is Fieldsshow, this array holds some of the fields in the Fields_all that are used to display the fields. For the edit page, the field to be displayed is placed in the $field_edit for the creation of the page, the field to be displayed is placed in the $field_create, now inherit the Formcontroller class just need to configure it well;
<?php//Account Management System class Badmincontroller extends formcontroller{public function __construct () {$this-&          Gt;model = ' \badmin '; $this->fields_all = [' id ' = = [' Show ' = ' + ', '], ' Nickname ' + [' show ' = ' nickname ', ' search ' = ' = ' nickname like CONCAT ('% ',?, '%               '), ' username ' + [' show ' = ' username ',],                    ' Email ' + [' show ' = ' email ',], ' password ' and ' = [               ' Show ' = ' password ', ', ' created_at ' and ' show ' = ' Create Time ',           ], ' updated_at ' = [' show ' = ' update Time ',],];          $this->fields_show = [' id ', ' nickname ', ' username ', ' email ', ' created_at ']; $this->fields_edit = ['Nickname ', ' username '];          $this->fields_create = [' nickname ', ' username ', ' email ', ' password '];     Parent::__construct (); }} 
Define the model in the constructor,Fieldsall, Fieldsall,fields_show, F i e l D s e d I t , "> f Iel d Sed it fieldsedit,fields_create. For the field name Fields_all,key for the database, value is an array, show is the display name, and if you want the field to be searchable on the list page, set the Search property. Routes are routed below, with three laravel in the middle of the route:
Route::get (' Order/{id} ', [' as ' = ' order.detail ', ' uses ' = ' [email protected] '); Route::controller (' preview ', ' Previewcontroller '); Route::resource (' badmin ', ' Badmincontroller ');

The third kind has already completely defined the deletion to change the operation, looks can save me a lot of things, OK, I will use this resource to do. So in route.php, I just need to define this one.
Admin Account Management Route::resource (' badmin ', ' Badmincontroller ');

Controller to write Fromcontroller in the resource method according to Laravel's resource definition, need to fill the method is: I am accustomed to in the constructor, such as input, the global definition of things are share into the template, The code is as follows:
 Public Function __construct () {///TODO: Make some basic judgments, throw an exception if not, $route = Route::cur          Rentrouteaction ();          List ($this->controller, $action) = explode (' @ ', $route);           View::share (' controller ', $this->controller);          $fields _show = Array ();          foreach ($this->fields_show as $field) {$fields _show[$field] = $this->fields_all[$field];           } view::share (' Fields_show ', $fields _show);          $fields _edit = Array ();          foreach ($this->fields_edit as $field) {$fields _edit[$field] = $this->fields_all[$field];           } view::share (' Fields_edit ', $fields _edit);          $fields _create = Array ();          foreach ($this->fields_create as $field) {$fields _create[$field] = $this->fields_all[$field];           } view::share (' Fields_create ', $fields _create);     View::share (' Input ', input::all ()); }

This is where you put the controller outside so you can use it in view such as: action (CONTRol le r . ′@dest< Span id= "mathjax-span-90" class= "Mi" >ro y ′ controller.′ @destroy ′,model->id),The path defines the index function:
      Public Function Index ()     {          $model = new $this->model;          $builder = $model->orderby (' id ', ' desc ');           $input = Input::all ();          foreach ($input as $field = + $value) {               if (empty ($value)) {                    continue;               }               if (!isset ($this->fields_all[$field])) {                    continue;               }               $search = $this->fields_all[$field];               $builder->whereraw ($search [' Search '], [$value]);          }          $models = $builder->paginate (a);           Return View::make (' Form.index ', [               ' models ' = ' $models,          ]);     

$builder in Laravel is so damn good, for the search here, I use Whereraw to prepare query. Here is a point, before the Fields_all design, I defined the direct is a ' search ' = ' nickname like CONCAT ('% ',?, '% ') "There are many design methods when defining search fields, such as defined as

' Search ' = [     ' type ' = ' like ',     ' value ' = '%?% ']

But given that the fromcontroller are all programmers, the search here uses pre-processed statements directly and then uses Whereraw directly in index, which increases the readability of the configuration file. Here is the CREATE function:
     Public function Create ()     {          return view::make (' form.create ', []);     

Store function:
     Public function Store ()     {          $model = new $this->model;          $model->fill (Input::all ());          $model->save ();          Return Redirect::to (Action ($this->controller. ' @index '));     

The model fill here is not very simple, cool to explode. Of course, the model still needs to define the Fillable field edit,update,destory function to do the same.
     Public function edit ($id)     {          $model = new $this->model;          $model = $model->find ($id);          Return View::make (' Form.edit ', compact (' model '));     }      Public Function Update ($id)     {          $model = new $this->model;          $model = $model->find ($id);          $model->fill (Input::all ());          $model->save ();           Return Redirect::to (Action ($this->controller. ' @index '));     }      Public function Destroy ($id)     {          $model = new $this->model;          $model->destroy ($id);                   Return Redirect::to (Action ($this->controller. ' @index '));     

View below is the view's writing. View about just three pages, List page, edit page, create page List page Note: 1 use Laravel to bring the page, remember to bring the input parameters of this page, this time, the constructor share input is very useful {{models−>appends( models−>appends (Input)->links ()}}2 You can use the from operation that comes with laravel, such as the delete operation because of the need to invoke the Delete method of HTTP, you can write this
                 {{Form::open (                  ' id ' = ' = ' delete_{$model->id} ',                  ' url ' = = action ($controller. ' @destroy ', $model->id),                  ' class ' = ' Dropdown-toggle ')}}                    {{Form::hidden (' _method ', ' DELETE ')                  }} {{form::close ()}}

Actually write the delete also line, is in the from form to pass a _method hidden field 3 search directly using a form can be done
     <form class= "Form-inline" role= "form" action= "{{action ($controller. ' @index ')}} ">          @foreach ($fields _show as $field = $field _info)            @if (isset ($field _info[' search '            )) <div class= "Form-group" >              <label class= "col-sm-3 control-label" >{{$field _info[' show ']}}</label >              <div class= "col-md-3" >                <input name= "{{$field}}" type= "Input" class= "Form-control" placeholder = "Value=" @if (Isset ($input [$field]) {{$input [$field]}} @endif ">              </div>            </div>            @endif          @endforeach          <input type= "Submit" class= "btn btn-success" value= "Query"/>        </form>

Edit page and create page simple enough to just need a form to get it done.
  <form class= "Form-horizontal"          role= "form"          action= "{{action ($controller. "@update", $model->id)}} "method= ' POST ' >          <input type=" hidden "name=" _method "value=" PUT ">            @ foreach ($fields _edit as $field = $field _info)            <div class= "Form-group" >              <label class= "col-sm-2 Control-label ">{{$field _info[' show ']}}</label>              <div class=" col-sm-10 ">                <input name=" {{ $field}} "type=" text "class=" Form-control "placeholder=" "value=" {{$model, $field}} ">              </div>            </div>            <div class= "line line-dashed line-lg pull-in" ></div>            @endforeach            <div class= "Col-sm-4 col-sm-offset-2" >              <button type= "Submit" class= "BTN btn-primary" > Submit </button>            </div>          </form>

Remember that the update operation in resource is to use the Put method, deleting the operation to use Delete mode. As for the view template, I am using a template called Notebook, it is based on bootstrap, you can also use other better-looking templates to write.

Laravel additions and deletions to search

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.