Jafo232's avatar

Where In Array

I have some columns that contain an array of ids (as defined in the model) based on a remote database I only have access to via an API. Example, myitems column:

["359045532","359079612","359079372","359081292","359081052","359086332","359086092","359111892","359111652"]

How do you query to find out if a single ID exists in that column?

0 likes
17 replies
Snapey's avatar

You will need to use a where query with a 'like' condition and your ID surrounded by percent signs. It won't be fast;

$item = "359081292";

Model::where('myitems', 'like', "%{$item}%")->get();

Its not a very good way to store the item id's obviously

1 like
KenoKokoro's avatar

What type of database do you use, and what type of column?

kamleshpant's avatar

You can simply use the following code

$k = ["359045532","359079612","359079372","359081292","359081052","359086332","359086092","359111892","359111652"];

Modal::whereIn('myitems', $k)->get(); 
5 likes
rebirthtobi's avatar

if you want to query a model use

$ids =
 ["359045532","359079612","359079372","359081292","359081052","359086332","359086092","359111892","359111652"];

Modal::whereIn('column', $ids)->get(); 

But if you want to know if an id exist in the array, do

if(in_array($idToCheck, $ids)){
    //returns true if the $idToCheck exist
}
else{
    //returns false
}
Jafo232's avatar

whereIn does not work.. Understand, the data in each ROW is an array.

rebirthtobi's avatar

If the column contains an array of values, the you will need to get the column values then loop through the ids and check if its in array returned by model

$ids =
 ["359045532","359079612","359079372","359081292","359081052","359086332","359086092","359111892","359111652"];

$models = Model::select('column_array')->get();

$model_values = [];

foreach($models as $model){
    $model_values = array_merge($model_values, $model->column_array);
}

foreach($ids as $id){
    if(in_array($id, $model_values)){
        //returns true if the $idToCheck exist
    }
    else{
        //returns false
    }
}

But mind you, you may need to use mutators in your model

Snapey's avatar

boy! I hope you are not still struggling 6 months on. ;-) Can't see any of these answers improving on what I suggested...

mehrancodes's avatar

@Snapey Thanks for your answer, but what's your idea to store an array in the database to make it faster when doing a query?

Cronix's avatar

It's simple. You don't. Each of those things should be stored in a separate table using a one to many or many to many relationship. That's how relational databases are supposed to work. Then it's simple to query the table to see if the record exists.

1 like
cmdobueno's avatar

Yeah... relationships are your best friend.

Example:

My table users has two columns an ID and a Name

I want to have friends for users...

YOUR METHOD add column fiend_ids and store this data as json

RELATIONAL METHOD: create table called user_fiends columns: user_id & friend_id this is a primary key user_id & friend_id are foreign keyed to the users table with whatever action you want...

I hope that makes sense... should be easy enough to understand. You can then do some cool relationships and magical attaching and detaching (depending on your relationships and all that)

1 like
HDS2019's avatar

Create your array and search your array in database: $item_array = ( 0 => 1, 1 => 2, 2 => 3 );

$items = Item::whereIn('item_id', $item_array)->orderBy('id', 'ASC')->paginate(10);

1 like
mepsd's avatar

i think the where like is the best way i'e if there is values like ["359045532","359079612","359079372","359081292","359081052","359086332","359086092","359111892","359111652"]; you can add qoutation in order to not getting other data like Model::where('myitems', 'like', "%"{$value}"%")->get(); this is how far we can go as of now

mabdullahsari's avatar

@mepsd I'm sure he is still stuck on the same problem 4 years later?

Surely? He is still waiting for that one and only savior.

1 like
Ferid's avatar

I solved like that Service

$items = $this->model
            ->query()
			->when($filters['game_category'] != null, function ($q) use ($filters) {
       					foreach($filters['game_category'] as $c){
               						$q->categoryLike($c);
        					}
			})->get();					

Game model

public function scopeCategoryLike($q, $category)
    {
        $q->where('game_categories','like','%'.$category.'%');
    }

Please or to participate in this conversation.