CRUD
CRUD copied to clipboard
Relationship
Bug report
What I did
CRUD::addColumn([
'label' => "Uporabnik",
'name' => 'numberable',
'entity' => 'numberable',
'attribute' => 'full_name',
'wrapper' => [
'href' => function ($crud, $column, $entry, $related_key) {
if($entry['numberable_type'] == 'App\Models\Place'){
return backpack_url('place/'.$related_key.'/show');
}
elseif($entry['numberable_type'] == 'App\Models\Employee'){
return backpack_url('employee/'.$related_key.'/show');
}
},
],
'searchLogic' => function ($query, $column, $searchTerm) {
$query->orWhereHas('numberable', function ($q) use ($column, $searchTerm) {
$q->where('full_name', 'like', '%'.$searchTerm.'%');
});
}
]);
What I expected to happen
What happened
What I've already tried to fix it
??
Is it a bug in the latest version of Backpack?
After I run composer update backpack/crud the bug... is it still there?
this heppend when i updated to backpack v6
Backpack, Laravel, PHP, DB version
When I run php artisan backpack:version the output is:
PHP VERSION:
PHP 8.1.17 (cli) (built: Mar 14 2023 23:07:43) (ZTS Visual C++ 2019 x64) Copyright (c) The PHP Group Zend Engine v4.1.17, Copyright (c) Zend Technologies
LARAVEL VERSION:
10.44.0.0
BACKPACK PACKAGE VERSIONS:
backpack/activity-log: 1.0.0 backpack/backupmanager: v5.0.0 backpack/basset: 1.2.2 backpack/crud: 6.0.0 backpack/filemanager: 3.0.7 backpack/generators: v4.0.3 backpack/langfilemanager: 5.0.1 backpack/permissionmanager: 7.1.1 backpack/pro: 2.0.4 backpack/settings: 3.1.0 backpack/theme-coreuiv2: 1.1.4
Hey @alien-rat
I tried something similar on my CRUD, which worked:
CRUD::column([
'label' => "locations",
'name' => 'locations',
'entity' => 'locations',
'attribute' => 'name',
'wrapper' => [
'href' => function ($crud, $column, $entry, $related_key) {
return backpack_url('location/'.$related_key.'/show');
},
],
]);
I tested it using an accessorfull_name(just in case you are using it), but that also worked.
From your screenshot it looks like full_name is returning the model obj; $this.
Please check & let me know
I think the problem might be the logic itself. Do you have other possible values for $entry['numberable_type'] than Place and Employee? Could it be that this is not the correct property or you should not access it with $entry['numberable_type'] but with $entry->numberable_type?
As a test I would suggest that you do
if ($entry['numberable_type'] == 'App\Models\Place') {
return backpack_url('place/' . $related_key . '/show');
} elseif ($entry['numberable_type'] == 'App\Models\Employee') {
return backpack_url('employee/' . $related_key . '/show');
} else {
return 'test';
}
// or
if ($entry->numberable_type == 'App\Models\Place') {
return backpack_url('place/' . $related_key . '/show');
} elseif ($entry->numberable_type == 'App\Models\Employee') {
return backpack_url('employee/' . $related_key . '/show');
} else {
return 'test';
}
This way you will know for sure what is not working as you expect.
You can even directly use dd($entry) or dd($entry['numberable_type']) to see what it contains and modify your code appropriately. You could also do the comparison like $entry['numberable_type'] == Place::class (and import the model).
I tested it, but the logic is ok, I only have two numberable, place and employee. The second problem is that $related_key is empty i think morph not working, 1 to n works.
PHP VERSION:
PHP 8.1.17 (cli) (built: Mar 14 2023 23:07:43) (ZTS Visual C++ 2019 x64) Copyright (c) The PHP Group Zend Engine v4.1.17, Copyright (c) Zend Technologies
LARAVEL VERSION:
10.44.0.0
BACKPACK PACKAGE VERSIONS:
backpack/activity-log: 1.0.0 backpack/backupmanager: v5.0.0 backpack/basset: 1.2.2 backpack/crud: 6.6.3 backpack/filemanager: 3.0.7 backpack/generators: v4.0.3 backpack/langfilemanager: 5.0.1 backpack/permissionmanager: 7.1.1 backpack/pro: 2.0.11 backpack/settings: 3.1.0 backpack/theme-coreuiv2: 1.2.3
Hello @alien-rat thanks for the report.
Indeed we have a small issue here. The MorphTo relation is not using the correct configuration as a "select" column.
I've just pushed the fix in backpack/pro v2.1.4, it should take a while for our private repository to pick the change and you can then get the fix with a composer update.
In the meanwhile just for clarification, you could also used a "text" column, and direct access the properties. Something like the following:
$this->crud->addColumn([
'name' => 'morphable.relatedAttribute',
'type' => 'text',
'wrapper' => [
'href' => function ($crud, $column, $entry) {
if($entry->morphable_type === (new \App\Models\MyModel)->getMorphClass()){
return backpack_url('entity/'.$entry->morphable_id.'/show');
}
},
],
]);
Notice the usage of getMorphClass() as it allows you to create a morph map to avoid storing App\Models\Namespaces in your database as it is a bad practice.
Let me know if that solved the issue for you 👍
Cheers
great, i have to buy new version...
Hi there,
We noticed your package download has failed.
That's because... you don't have access to backpack/pro version 2.1.4. That version was released on 2024-02-16, after your period of updates had expired. Please require "backpack/pro":"2.0.11" in your composer.json, which is the latest version you have access to.
To gain access to this newer version (and 12 more months of updates and upgrades), you can purchase another year of "backpack/pro" updates on backpackforlaravel.com.
Cheers!
@alien-rat hey there. You don't need to buy a new version. I gave you a workaround solution that works just fine as well, it's not so intuitive but should be enough to display the information you need.
Cheers
nop, this doesn't work.. `$this->crud->addColumn([ 'name' => 'numberable', 'type' => 'text', 'attribute' => 'full_name', 'wrapper' => [ 'href' => function ($crud, $column, $entry) { if($entry->numberable_type === (new \App\Models\Place)->getMorphClass()){ return backpack_url('place/'.$entry->numberable_id.'/show'); } if($entry->numberable_type === (new \App\Models\Employee)->getMorphClass()){ return backpack_url('employee/'.$entry->numberable_id.'/show'); }
},
],
]);`
Hey @alien-rat I am sorry, but what you tried is nothing close to what I suggested you.
Please read carefully what I've suggested and try again.
$this->crud->addColumn([
'name' => 'numberable.full_name'
'type' => 'text'
Cheers
@pxpm sorry, i missed that.
thx, it works.
Hey @alien-rat it doesn't help much that kind of reports, I have no information on the issue, it seems odd to me that it works in some pages and not in others. In case it only worked in one page, and none of the others I could suspect it's a Backpack issue, but in this case I wonder if isn't some loaded models on those pages that break the page.
An error stack trace would be helpful in case you need my further assistance.
Cheers