PHP code example of netcore / module-crud

1. Go to this page and download the library: Download netcore/module-crud 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/ */

    

netcore / module-crud example snippets

 


namespace App\Http\Controllers;

use App\Article;
use Modules\Crud\Traits\CRUDController;

class ArticlesController extends Controller
{
    use CRUDController; // <- This trait is  __construct()
    {
        $this->model = app(Article::class); // <- Set model.
    }
}
 


namespace App;

use Illuminate\Database\Eloquent\Model;
use Modules\Crud\Traits\CRUDModel;

class Article extends Model
{
    use CRUDModel; // <- This is .. Relations etc ...
}
 


namespace App\Presenters;

use Modules\Crud\Contracts\DatatablePresenterContract;

class ArticleModuleDatatablePresenter implements DatatablePresenterContract
{
    /**
     * Get the datatable columns config/mapping.
     *
     * @return array
     */
    public function getDatatableColumns(): array
    {
        return [
            'id'           => 'ID',
            'is_published' => 'Is published',
            'title'        => [
                'title'      => 'Article title', // column title
                'data'       => 'title', // column data field
                'name'       => 'translations.title', // SQL column name
                'searchable' => true, // Is searchable?
                'orderable'  => true, // Is orderable?
            ],
            'created_at'   => 'Added at',
        ];
    }

    /**
     * Get the list relations that should be eager loaded.
     *
     * @return array
     */
    public function eagerLoadableRelations(): array
    {
        return ['translations'];
    }

    /**
     * Get the columns that should not be escaped.
     *
     * @return array
     */
    public function getRawColumns(): array
    {
        return ['is_published'];
    }

    /** -------------------- Column modifiers -------------------- */

    /**
     * Modify is_published column.
     *
     * @param $row
     * @return string
     */
    public function isPublishedModifier($row)
    {
        $labelClass = $row->is_published ? 'success' : 'danger';
        $labelText = $row->is_published ? 'Yes' : 'No';

        return "<span class=\"label label-{$labelClass}\">{$labelText}</span>";
    }
}
 
class Article extends Model
{
    use CRUDModel;

    ...
    
    /**
     * Get the presenter class for datatable.
     *
     * @return string
     */
    public function getDatatablePresenter(): string
    {
        return \App\Presenters\ArticleModuleDatatablePresenter::class;
    }
}

class FoodType extends Model
{
    use Translatable, SyncTranslations, CRUDModel;

    protected $fillable = [
        'example_1',
        'example_2',
        'example_3',
        'example_4',
    ];

    /**
     * @var string
     */
    public $translationModel = FoodTypeTranslation::class;

    /**
     * @var array
     */
    public $translatedAttributes = [
        'name',
    ];

    /**
     * @var array
     */
    protected $with = ['translations'];
}

class TestFiles extends Model implements StaplerableInterface
{
    use CRUDModel, EloquentTrait;

    /**
     * @var array
     */
    protected $fillable = ['image'];

    /**
     * TestFiles constructor.
     * @param array $attributes
     */
    public function __construct(array $attributes = array())
    {
        $this->hasAttachedFile('image', [
            'url'    => '/uploads/portfolio/:id_partition/:filename',
        ]);

        parent::__construct($attributes);
    }

}