setModel

setModel(\GroceryCrud\Core\Model\ModelInterface $model);

By default GroceryCRUD is using a Model that already automated for you pretty much everything (check \GroceryCrud\Core\Model for more). However there are many cases that there are too complex queries that you need to have. In that case you can add a custom model to execute. The syntax is simple:

$model = new customModel($db);
$crud->setModel($model);

Example

A more specific example can be the below:

$crud->setModel(new customModel($db));

$crud->setTable('customers');
$crud->setSubject('Customer', 'Customers');
$crud->columns(['customerName', 'country', 'state', 'addressLine1']);

$output = $crud->render();

where $db is the config variable for the database and where customModel is:

<?php 

use GroceryCrud\Core\Model;

class customModel extends Model {

    public function extraWhereStatements($select)
    {
        $select->where("customers.country = 'USA'");
        return $select;
    }

}

Note: There is a separate section that you can find at the Advanced Examples How to create a custom model that is covering how to create your own unique models for advanced needs.

This example is to help you understand only the basic usage

The result of the above code is: