PHP code example of vuthaihoc / rapyd

1. Go to this page and download the library: Download vuthaihoc/rapyd library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

vuthaihoc / rapyd example snippets


#  /app/Http/rapyd.php
// Route::controller('rapyd-demo','\Zofe\Rapyd\Demo\DemoController');

   $grid = \DataGrid::source(Article::with('author'));  //same source types of DataSet
   
   $grid->add('title','Title', true); //field name, label, sortable
   $grid->add('author.fullname','author'); //relation.fieldname 
   $grid->add('{{ substr($body,0,20) }}...','Body'); //blade syntax with main field
   $grid->add('{{ $author->firstname }}','Author'); //blade syntax with related field
   $grid->add('body|strip_tags|substr[0,20]','Body'); //filter (similar to twig syntax)
   $grid->add('body','Body')->filter('strip_tags|substr[0,20]'); //another way to filter
   $grid->edit('/articles/edit', 'Edit','modify|delete'); //shortcut to link DataEdit actions
   
   //cell closure
   $grid->add('revision','Revision')->cell( function( $value, $row) {
        return ($value != '') ? "rev.{$value}" : "no revisions for art. {$row->id}";
   });
   
   //row closure
   $grid->row(function ($row) {
       if ($row->cell('public')->value < 1) {
           $row->cell('title')->style("color:Gray");
           $row->style("background-color:#CCFF66");
       }  
   });
   
   $grid->link('/articles/edit',"Add New", "TR");  //add button
   $grid->orderBy('article_id','desc'); //default orderby
   $grid->paginate(10); //pagination

   view('articles', compact('grid'))



  #articles.blade.php  

  {!! $grid !!} 


   ...
   $grid->add('title','Title', true)->style("width:100px"); //adding style to th
   $grid->add('body','Body')->attr("class","custom_column"); //adding class to a th
   ...
    //row and cell manipulation via closure
    $grid->row(function ($row) {
       if ($row->cell('public')->value < 1) {
           $row->cell('title')->style("color:Gray");
           $row->style("background-color:#CCFF66");
       }  
    });
    ...

   ...
   $grid->add('title','Title');
   $grid->add('body','Body')
   ...
   $grid->buildCSV();  //  force download 
   $grid->buildCSV('export_articles', 'Y-m-d.His');  // force download with custom stamp
   $grid->buildCSV('uploads/filename', 'Y-m-d');  // write on file 
    ...
    $grid->buildCSV('uploads/filename', 'Y-m-d', false); // without sanitize cells
    
    $as_excel = ['delimiter'=>',', 'enclosure'=>'"', 'line_ending'=>"\n"];  
    $grid->buildCSV('uploads/filename', 'Y-m-d', true, $as_excel); // with customizations
    

   //start with empty form to create new Article
   $form = \DataForm::source(new Article);
   
   //or find a record to update some value
   $form = \DataForm::source(Article::find(1));

   //add fields to the form
   $form->add('title','Title', 'text'); //field name, label, type
   $form->add('body','Body', 'textarea')->rule('me','Categories','tags'); //tags field
   $form->add('map','Position','map')->latlon('latitude','longitude'); //google map


   //you can also use now the smart syntax for all fields: 
   $form->text('title','Title'); //field name, label
   $form->textarea('body','Body')->rule('

   #article.blade.php

  {!! $form !!}

     ...
     $form->build();
     view('article', compact('form'))
 

   #article.blade.php
    {!! $form->header !!}

        {!! $form->message !!} <br />

        @if(!$form->message)
            <div class="row">
                <div class="col-sm-4">
                     {!! $form->render('title') !!}
                </div>
                <div class="col-sm-8">
                    {!! $form->render('body') !!}
                </div>
            </div> 
            ...
        @endif

    {!! $form->footer !!}

   //simple crud for Article entity
   $edit = \DataEdit::source(new Article);
   $edit->link("article/list","Articles", "TR")->back();
   $edit->add('title','Title', 'text')->rule('smart syntax for all fields: 
   $edit->textarea('title','Title'); 
   $edit->autocomplete('author.name','Author')->search(['firstname','lastname']);
   
   return $edit->view('crud', compact('edit'));


   #crud.blade.php
 
  {!! $edit !!} 

   $filter = \DataFilter::source(new Article);

   //simple like 
   $filter->add('title','Title', 'text');
          
   //simple where with exact match
   $filter->add('id', 'ID', 'text')->clause('where')->operator('=');
          
   //custom query scope, you can define the query logic in your model
   $filter->add('search','Search text', 'text')->scope('myscope');
      
   //cool deep "whereHas" (you must use DeepHasScope trait bundled on your model)
   //this can build a where on a very deep relation.field
   $filter->add('search','Search text', 'text')->scope('hasRel','relation.relation.field');
   
   //closure query scope, you can define on the fly the where
   $filter->add('search','Search text', 'text')->scope( function ($query, $value) {
         return $query->whereIn('field', ["1","3",$value]);
   })
   
   $filter->submit('search');
   $filter->reset('reset');
   
   $grid = \DataGrid::source($filter);
   $grid->add('nome','Title', true);
   $grid->add('{{ substr($body,0,20) }}...','Body');
   $grid->paginate(10);

   view('articles', compact('filter', 'grid'))

   # articles.blade
   
   {!! $filter !!}  
   {!! $grid !!}


    // the root node won't appear, only its sub-nodes will be displayed.
    $root = Menu::find(1) or App::abort(404);

    $tree = \DataTree::source($root);
    $tree->add('title');
    $tree->edit("/menu/edit", 'Edit', 'modify|delete');
    $tree->submit('Save the order');
    return view('menu-list', compact('tree'));

    use Zofe\Rapyd\Facades\DataSet;
    use Zofe\Rapyd\Facades\DataGrid;
    use Zofe\Rapyd\Facades\DataForm;
    use Zofe\Rapyd\Facades\DataForm;
    use Zofe\Rapyd\Facades\DataEdit;
    ..
    DataGrid::source()... 

    Class MyDataGrid extends Zofe\Rapyd\DataGrid\DataGrid {
    ...
    }
    Class MyDataEdit extends Zofe\Rapyd\DataEdit\DataEdit {
    ...
    }
    ..
    MyDataGrid::source()