Rapyd Cheatsheet

Rapyd is a simple package for laravel with the aim to let you develop crud interfaces in few lines of code. Few widgets: tables, grids, forms with simple syntax, simple behavior, simple output.

DataFilter

  • Initiate

    $filter = DataFilter::source(Article::with('author','categories'));
  • Add attributes

    $filter->attributes(array("class"=>"form"));
  • Autocomplete tag

    $filter->add('categories.categoryName','Categories','tags');
  • Checkbox

    $filter->add('live','Live Info','checkbox');
  • Free text search

    $filter->add('email_address','Email Address', 'text');
  • Date range

    $filter->add('publication_date','publication date','daterange')->format('m/d/Y', 'en');
  • Buttons

    $filter->submit('search');
    $filter->reset('reset');
    $filter->build();

DataGrid

Set a source with eager load

$grid = DataGrid::source(TableModel::with("AnotherTableMethodFromTableModel"));

 or without

 $grid = DataEdit::source(new TableModel);

Adding item

$grid->add(data,heading,orderby field);

Specificying Eloquent fields

Field Explanation
$grid->add(title); eloquent field
$grid->add(category.name); relationshop field
$grid->add({{$title}}) eloquent field
$grid->add($row->title) eloquent field
$grid->add($row->method()) eloquent method

Adding a default value for insert

->insertValue('1');

Adding a list (e.g list of categories, tags)

 $grid->add('{{ implode(", ", $categories->lists("categoryName")) }}','Categories?);

Adding item from joined table

$grid = DataGrid::source(TableModel::with("AnotherTableMethodFromTableModel"));
$grid->add({{$AnotherTableMethodFromTableModel->id}},"Id");

Joined/Relation table with null values

Use the ampersand @

$grid->add('{{@$row->user->email}}','User');

Adding custom column to grid

$grid->add('custom','Custom')->cell(function ($value) {
   // you can do for example format $value and/or
    return 'custom.. code or view';
});

or using blade syntax / php code

$grid->add('{{ strtoupper($fieldname) }}','Custom');

Formatting display of field data

$grid->add('body', 'Body')->filter('strip_tags|substr[0,20]');  //filters
$grid->add('body|strip_tags|substr[0,20]','Body');  //inline filters
$grid->add('ucfirst(substr( {{ $body }}, 0,100))', 'Body');  //inline blade

/and closure on value
$grid->add('body', 'Body')->cell( function ($value) {
                        return substr(strip_tags($value), 0, 20);
 });

Displaying data depending on cell value

For example view comments link if status is 2,3 or 5:

 $grid->add('status','Options')->cell( function( $value, $row) {
        return (in_array($value->id,[2,3,5])) ? '<a href="/engage/edit?id=' .   $row->id . '">View Comments</a>' : '';
    });

Note $row callback was added in 2.2.1 - if you don't have the latest version you'll need to update the source code (not recommended but if you're stuck on earlier Laravel you won't have much choice:

 +   
 +   //cell closure
 +   $grid->add('revision','Revision')->cell( function( $value, $row) {
 +        return ($value != '') ? "rev.{$value}" : "no revisions for art. {$row->id}";
 +   });
 +   

https://github.com/zofe/rapyd-laravel/commit/62e87122b21366ed7595eb090b27cd54992887b8

Formatting html / css

$grid->row(function ($row) {
       if ($row->cell('id')->value == 20) {
           $row->style("background-color:#CCFF66");
       } elseif ($row->cell('id')->value > 15) {
           $row->cell('title')->style("font-weight:bold");
           $row->style("color:#f00");
       }
    });

 Ordering

$grid->orderBy('queue_id','desc');

Pagination

$grid->paginate(10);

Editing (DataEdit)

Field types

Field Parameters Example
Text field name, label, type $edit->add('title','Title', 'text');
Checkbox field name, label, type $edit->add('public','Public','checkbox'); Checkbox field name, label, type $edit->add('public','Public','checkbox');
Checkbox group $edit->add('categories','Categories','checkboxgroup')->options(Category::lists("name", "category_id")->all());
Radiobox $edit->add('gender','Gender','radiogroup')->option('F','Female')->option('M','Male');
Textarea $form->add('body','Body', 'textarea')
Redactor Textarea $form->add('body','Body', 'redactor');
Textarea $form->add('body','Body', 'textarea')
Textarea $form->add('body','Body', 'textarea')
File upload $form->add('download','Attachment', 'file')->rule('mimes:pdf')->move('uploads/pdf/');
Image upload $form->add('photo','Photo', 'image')->move('uploads/images/')->preview(80,80);
Date $form->add('publication_date','pub.date','date')->format('d/m/Y', 'it');
Date Range $filter->add('publication_date','pub.date','daterange')->format('d/m/Y', 'it');
Simple Autocomplete $form->add('author_id','Author','autocomplete')->options(Author::lists('firstname', 'id'));
autocomplete with relation.field to manage a belongsToMany $form->add('author.fullname','Author','autocomplete')->search(array("firstname", "lastname"));
Hidden Fields $filter->add('myfield','','auto')->insertValue('myvalue');
Hidden Fields $filter->add('anotherfield','','auto')->updateValue('anothervalue');
Content $edit->add('author1','Author1','container')->content('edited by: {{ $model->author->fullname }}');
Blade View path of a blade view with $model injected $edit->add('options','Author2','container')->view('voting.options');
iFrame $edit->add('id','','iframe')->src('/engage/votingquestions?questionId=' . $questionId);

Set a field to read only

->mode('readonly')

Rules

Set a field to be required

->rule('required');

Set a file to be of a certian type

->rule('mimes:pdf')

Remove field

$edit->remove('image');

Set a field

$edit->set('s3_path', $s3FilePath); 

Using data from an array

$ages = array (
        'all'  => 'All',
         'under_18' => 'Under 18',
         'over_18' => 'Over 18',
);

$edit->add('age_range','Age range','select')->options($ages);

$age_range = $request->input('age_range');

Images

  • Changing url

    ->move('uploads/images/)->webPath('/uploads')
  • note: must come after ->move

  • Moving a file

    ->move('uploads/images/');
  • Renaming a file

    ->move('uploads/images/','customname.jpg');
  • Preview

    $form->add('photo','Photo', 'image')->move('uploads/demo/')->fit(240, 160)->preview(120,80);
  • Resize $form->add('photo','Photo', 'image')->move('uploads/demo/')->resize(240, 160)->preview(120,80);

  • Making Preview Link work

Specify web path prefix explicity:

`  ->webPath(PATH)`

Examples

Closure (to access Intervention)

    $form->add('photo','Photo', 'image')->move('uploads/demo/')->image(function ($image)            {
     // $image is an instance of \Intervention\Image\ImageManagerStatic 
     // with uploaded image, you can do here something different...  
    // remember to do also:  $image->save('filename');
    })->preview(120,80);

    $edit->add('image','Image','image')->image(function($image){
        $name = $image->filename.time();
        $extension = $image->extension;
        $image->widen('800',function($constraint){$constraint->upsize();});
        $image->save("public/uploads/{$name}_width800.{$extension}");
        }

Validation

$form = DataForm::source(Article::find(1));
$form->validator = Validator::make(Input::all(), $rules, $messages);

or trigger a customer error server side:

$form->passed(function () use ($form) {
        if (someting...) {
                 $form->error("..mm there is a problem !");
        } else {
                $form->message("ok saved!");
        }
        $form->link("/mycontroller/form","back to the form");
 });

source

 Redirect somewhere else after editing

return Redirect::to(\Zofe\Rapyd\Persistence::get("authors/list"));

Doing something with the data before saving

Use Eloquent!

Eg default if no value:

 protected $attributes = array(
    'mainMedia' => '',
    'mainImage' => '');

Replace field value:

 public function setMainImageAttribute($value)
{
    if ($value == null) $this->attributes['mainImage'] = "xxxx";
            else $this->attributes['mainImage'] = value;
}

Doing something with the data after saving

DataEdit saves and then runs this closure:

$edit->saved(function() use ($edit)
{
    $form->field('fieldname')->updateValue($mycustomvalue);
        $form->model->fieldname = $mycustomvalue;
        $form->model->save();
});

Note $form->passed is an alias for $form->saved

Help

If you see {{$modelname->field}} in your grid it meams Rapyd wasn't able to connect to your join.