Conditional Relationships #1738
-
I have a unique case where I have a model with a relationship to two other models (ProductCatalog and Peripheral). In my model I have created a 'device' attribute which determines which relationship to return based on the id type of the item. Here's what it looks like: public function getDeviceAttribute() {
if($this->inventory_id > 0) {
return $this->product;
}
if($this->peripheral_id > 0) {
return $this->peripheral;
}
return null;
} In my table I can then create a column with 'device.name' and it works great. However when I try to search, the search throws an error that 'name' is not a column. I have tried various methods to get this to work with my table from using joins to using a conditional query in the datasource, I even tried create a 'device' method in the model but that didn't work either. Nothing seems to work and I'm starting to get a little frustrated. I'm thinking the answer is probably something simple that I'm overlooking, but if someone could point me in the right direction I would sure appreciate it. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 2 replies
-
So far the only luck I've had with figuring this out is to check if $this->search has a value and then do my own where checks in the dataSource builder. It works how I want, but there's got to be a better way of doing this as this defeats the whole point of having the 'searchable' functionality. |
Beta Was this translation helpful? Give feedback.
-
This case seems to be unique. Could you create an example repository working similarly so we can look into it more deeply? |
Beta Was this translation helpful? Give feedback.
-
Just checking if anyone had a chance to review this yet? Wondering if this problem is me overlooking something simple or if it is a limitation of Powergrid? |
Beta Was this translation helpful? Give feedback.
-
I achieved this just by adjusting the relationSearch method public function relationSearch(): array
{
return [
'peripheral' => 'name',
'product' => 'name'
];
} |
Beta Was this translation helpful? Give feedback.
I achieved this just by adjusting the relationSearch method