Thursday, March 9, 2017

Using Laravel 5.4 query builder with an array (json serialized) attribute

I'm stumped on this one. I can search an array of ids but I want to search the reverse. I have models with lists of ids as "with_ids" attribute and want to search similar to mongo db where id is in that array of ids. For example

db.conversations.find( { with_ids: { $in: [id] } } )

How do I do that with Laravel and mysql/Eloquent?

$conversations = Conversation::with('messages.user')->where('with_ids', $id)->orWhere('created_by', $id)->get();

it's the where('with_ids', $id) I can't figure out... Any suggestions??

To clarify further: I need to find if the user is participating in other conversations as well as the ones he created. The with_ids is a json serialized array f.ex [1,2,23,12] how do i search inside the array attribute?



via IamLasse

Advertisement