Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP-discussion] Allow show operation to use the panel query #5271

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions src/app/Http/Controllers/Operations/ShowOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,28 @@ public function show($id)
// get entry ID from Request (makes sure its the last ID for nested resources)
$id = $this->crud->getCurrentEntryId() ?? $id;

$this->data['crud'] = $this->crud;
$this->data['title'] = $this->crud->getTitle() ?? trans('backpack::crud.preview').' '.$this->crud->entity_name;

// get the info for that entry (include softDeleted items if the trait is used)
if ($this->crud->get('show.softDeletes') && in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses($this->crud->model))) {
$this->data['entry'] = $this->crud->getModel()->withTrashed()->findOrFail($id);
} else {
if (! $this->crud->get('show.softDeletes') || ! in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses($this->crud->model))) {
$this->data['entry'] = $this->crud->getEntryWithLocale($id);

return view($this->crud->getShowView(), $this->data);
}

$this->data['crud'] = $this->crud;
$this->data['title'] = $this->crud->getTitle() ?? trans('backpack::crud.preview').' '.$this->crud->entity_name;
// when enabled we will call the findOrFail() directly in the crud panel query, instead of the model query.
// this allow developers to setup constrains on the query that can be applied in multiple operations.
if ($this->crud->get('show.usePanelQuery')) {
$this->data['entry'] = $this->crud->query->withTrashed()->findOrFail($id);
// we will miss the "magic __call() to findOrFail" in the model when it's translatable,
// so we need to manually set the locale when needed.
$this->data['entry'] = $this->crud->setLocaleOnModel($this->data['entry']);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uuuu what does THIS button do?!

working-cartoon-network-gif

And why is this needed here? And only here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough. Writing a descriptive comment for you 🙏

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... something here is fishy to me. Why aren't we using getEntryWithLocale() and we are using setLocaleOnModel()? Looks to me like we're using a setter, instead of a getter.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this do the same thing or no?

Suggested change
$this->data['entry'] = $this->crud->setLocaleOnModel($this->data['entry']);
$this->data['entry'] = $this->crud->getEntryWithLocale($id);


return view($this->crud->getShowView(), $this->data);
}

$this->data['entry'] = $this->crud->getModel()->withTrashed()->findOrFail($id);

// load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package
return view($this->crud->getShowView(), $this->data);
Expand Down
2 changes: 2 additions & 0 deletions src/config/backpack/operations/show.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@

// When using tabbed forms (create & update), what kind of tabs would you like?
'tabsType' => 'horizontal', //options: horizontal, vertical

'usePanelQuery' => false,
];