Crud: Access the model itself on *CrudController

Created on 27 Dec 2018  路  2Comments  路  Source: Laravel-Backpack/CRUD

The scenario is I want to add an array as a default value for an element of $this->crud->addFields() method and this array should be filled with data of the current model.
so how can I access the current model in *crudController?

My solution is to use Route to find out what is the ID of the model : (my model is Coupon)

\Route::current()->parameters['coupon'])

that return 208 as an ID of the model from its path admin/coupon/208/edit

triage

Most helpful comment

Hi @apair ,

Inside the controller you'll always have access to $this->crud, so you can do $this->crud->getModel(). But if you need the actual entry, inside an operation like EDIT, you should be able to use $this->crud->getCurrentEntry().

If you need the CRUD inside a view (like a custom field view), you'll have $crud instead of $this->crud.

Hope it helps.
Cheers!

All 2 comments

Hello there! Thanks for opening your first issue on this repo!

Just a heads-up: Here at Backpack we use Github Issues only for tracking bugs. Talk about new features is also acceptable. This helps _a lot_ in keeping our focus on improving Backpack. If you issue is not a bug/feature, please help us out by closing the issue yourself and posting in the appropriate medium (see below). If you're not sure where it fits, it's ok, a community member will probably reply to help you with that.

Backpack communication mediums:

  • Bug Reports, Feature Requests - Github Issues (here);
  • Quick help (_How do I do X_) - Gitter Chatroom;
  • Long questions (_I have done X and Y and it won't do Z wtf_) - Stackoverflow, using the backpack-for-laravel tag;

Please keep in mind Backpack offers no official / paid support. Whatever help you receive here, on Gitter, Slack or Stackoverflow is thanks to our awesome _awesome_ community members, who give up some of their time to help their peers. If you want to join our community, just start pitching in. We take pride in being a welcoming bunch.

Thank you!

--
Justin Case
The Backpack Robot

Hi @apair ,

Inside the controller you'll always have access to $this->crud, so you can do $this->crud->getModel(). But if you need the actual entry, inside an operation like EDIT, you should be able to use $this->crud->getCurrentEntry().

If you need the CRUD inside a view (like a custom field view), you'll have $crud instead of $this->crud.

Hope it helps.
Cheers!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sonoftheweb picture sonoftheweb  路  3Comments

dividy picture dividy  路  3Comments

bastos71 picture bastos71  路  3Comments

M0H3N picture M0H3N  路  3Comments

sokvebolkol picture sokvebolkol  路  3Comments