0

After searching and trying for quite a while I am not sure how to tackle this issue. I cant really change the DB structure nor the specific function that is giving me problems.

Following is my problem: I have a table where certain contents are stored in an array. Said array contains the ids of entries in another table. The problem arises with the already built in search function in this system. It expects a laravel model / builder. This would work when I could call the standard relation functions but as the respective column is an array that one doesnt work.

Basically, I HAVE to call $model = Thing::where('id', '>=', 0) (for example) and pass that into the function, Thing::where('id', '>=', 0)->get() doesnt work. Or else I would have just looped over the $model an inserted the respective "relation" via foreach.

I cant use a function on the Model because obv the search function expects a builder and the function I call there is undefined. Relationships are also not working because they dont work on arrays.

Is there anything I am missing here?

N69S
  • 16,110
  • 3
  • 22
  • 36
Aescaru
  • 5
  • 4
  • 2
    It would take so much work to adapt a relation to a field containing an arrays of IDs as a string (feel free to take a look at this https://stackoverflow.com/questions/2696884/split-value-from-one-field-to-two) . It would be simpler to make a new table or pivot and add setter/getter (mutators) on the model to work with the legacy code. You will not find a built in solution for messy DB structure. – N69S Oct 25 '21 at 15:04
  • Sounds like you're trying to achieve some polymorphism in a weird way. Isn't there a way to change you're database structure and code to use Eloquent's polymorphism functions ? https://laravel.com/docs/5.7/eloquent-relationships#polymorphic-relationships – YannPl Oct 25 '21 at 15:06

0 Answers0