October CMS resources and help articles

Simple and to the point. Optimized by the community.

Access current Eloquent model in a backend form

5
by OFFLINE, last modified on August 8th, 2019

Sometimes you need to access attributes or methods of the current model in a backend form (let's say in a custom partial). You can access the model via the vars property:

$this->vars['formModel'];

Of course you have also access to all the model's properties and methods:

$this->vars['formModel']->id;
$this->vars['formModel']->title;
$this->vars['formModel']->customMethod();

Discussion

2 comments

1
ophi118
Post on August 5th, 2019 10:10 AM

Exactly as Daniel says. Additionally, $this->vars array is defined at the controller level and its content can be used directly within partials, so:

<?= $formModel->id ?> (defined in $this->vars['formModel'])

<?= $formRecordName ?>  (defined in $this->vars['formRecordName'])

will also work, you can add any custom values to this array in eg. controller's index method and use it directly as a variable in backend partials.

0
danielbidala
Post on May 20th, 2019 1:10 PM

Also you can use a simplier way:

$model; $model->id; $model->title; $model->customMethod();

We use cookies to measure the performance of this website. Do you want to accept these cookies?