PHP code example of fuzzyma / contao-eloquent-bundle

1. Go to this page and download the library: Download fuzzyma/contao-eloquent-bundle 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/ */

    

fuzzyma / contao-eloquent-bundle example snippets


public function registerBundles() {

    $bundles = [

        //...
        new WouterJ\EloquentBundle\WouterJEloquentBundle(), // we depend on it so we have to load it
        new Fuzzyma\Contao\EloquentBundle\ContaoEloquentBundle() // and that's ours

    ];

}


/**
 * Table tl_topics
 */
$GLOBALS['TL_DCA']['tl_topics'] = array
(
    'config' => array
    (
        // all the config
        'model' => Fuzzyma\Contao\EloquentExampleProjectBundle\Models\Topic::class
    )
	// Fields
	'fields' => array
	(
         // id, tstamp and so on and:
        'tags' => array
        (
            'label' => &$GLOBALS['TL_LANG']['tl_topics']['tags'],
            'inputType' => 'select',
            'eval' => ['multiple' => true],
            'eloquent' => [
                'relation' => 'belongsToMany',
                'model' => Fuzzyma\Contao\EloquentExampleProjectBundle\Models\Tag::class,
                'label' => '%name% [%comment%]'
                // 'medthod' => 'tags' // is automatically assumed
            ]
        ),
	)
);

namespace NamespaceToBundle\Models;

use Illuminate\Database\Eloquent\Model;
use Fuzzyma\Contao\EloquentBundle\Traits\ContaoFilesModelTrait; // only needed if you deal with files

class Topic extends Model
{

    use ContaoFilesModelTrait;

     // laravel use plural of class name as table. So in case you don't match the convention you can change it here
    protected $table = "topics";
    protected $guarded = [];
    public $timestamps = false; // we don't have timestamps but we could add them if we want

    // that method name must match the field name (without _id) or the specified method!!
    public function tags(){
        return $this->belongsToMany(Tag::class, 'tag_topic'); // you may want to name the pivot table here
    }

    // here the ContaoFilesModelTrait comes into place.
    // The Mutator will convert a call to $topic->thumbnail to a contao file model
    // of course that only works if you defined a field thumbnail in the dca
    public function getThumbnailAttribute($uuid){
        return $this->getContaoFile($uuid);
    }

    // same for setting a new file. Pass a FileModel, an UUID string or just binary
    public function setThumbnailAttribute($file){
        return $this->setContaoFile($file);
    }

}