Laravel whereIn list values not found in query? - php

When building a query to find multiple values from a model, eloquent would look something like this:
return Contact::whereIn('user_name', explode(',', $userNames)
But let's say that the value for userNames is ['foo', 'bar']; but I only have foo as a valid username, is there a way to get bar (all failed to find) out as part of the same query without having to compare the result ->get() against the request?

It's not possible to get the query to return the list of username that doesn't exist from the given condition. But you could do this.
$allUserNames = explode(',', $userNames);
$validUserNames = Contact::whereIn('user_name', $allUserNames)
->pluck('user_name')
->toArray();
$invalidUserNames = array_values(array_diff($allUserNames, $validUserNames));

You can use array diff to remove the unvalid value form the select region, and you lost the ->get() to get the results;
return Contact::whereIn('user_name', array_diff(explode(',', $userNames, ['bar']))->get()

Related

Laravel count distinct value and get as associative array of key value pair

I have a field called language in my table submission. Here the different user has submitted their problem with different language such as java(56) 5 times, CPP(45) 7 times, and python(71) 10 times.
I want to have a query in laravel with eloquent such that it returns an array or with key-value pair as
$user_lang = ['56'=>5,'45'=>7,'71'=>10]
here 56,45,71 are the id of the languages
If you have prepared data like count of how many times, you can simply use pluck method. eg
$user_lang = Language::where(<condition>)->pluck('times', 'id');
It will return the array as you desired.
If you do not have prepared count, then count it using group by and simply use same pluck method .
$result = Submission::selectRaw('id', 'COUNT(DISTINCT id) AS times'))
->groupBy(id)
->get();
$user_lang = [];
$result->map($result, function($item){
$user_lang[$item->id] = $item->times;
});

WhereIn Inside ->where() array - Laravel Query Builder

I'm trying use a whereIn inside a where array I am passing to Laravel query Builder:
$where = [['Participants.Client_Id','IN', $clientId]];
DB::table('Participants')->where($where)->get()
Something like is what I want to achieve, and I know there are works around such as using whereIn, but I'm sharing here a small piece of code to give you an idea, so I need to change the array to make it works as a whereIn, not changing the ->where to ->whereIn or ->whereRaw
DB::table('participants)->whereIn('Participants.Client_Id',$clientId)->get();
You must collect the IDs in the $clientId variables.
If I understand, you could do something like that :
$wheres = [['Participants.Client_Id','IN', [$clientId]]];
$query = DB::table('Participants');
foreach($wheres as $where) {
$query->where($where[0], $where[1], $where[2]);
}
$participants = $query->get();
As laravel document , you can use array in where and each element of this array must be a array with three value . So your $where variable is correct.
But as I searched in operator is not supported by query builder of where.

Doctrine 2 : Build dynamic query iterating on an array of parameters (AND conditions)

I'm trying to figure out how to correctly iterate on an array in a symfony 5 repository custom query.
I explain myself. App user may check several values on a page. I want to display objects that matches all (but not necessarily only) the checked values.
I easily catch an array of the values that I send to a controller with ajax but I encounter issues with my custom query.
Values matches the "label" parameter of a table (Tag) which is related (ManyToMany) to the table (Flash) I select with my query.
So far, I tried a lot of things, like :
public function getFlashesByTagsList($tags)
{
$qb = $this->createQueryBuilder('f');
$andX = $qb->expr()->andX();
$qb->join('f.tags', 't');
$qb->addSelect('t');
foreach($tags as $tag) {
$andX->add(
$qb->expr()->like('t.label', ':tag'),
$qb->setParameter(':tag', '%'.$tag.'%')
);
}
$qb->add('where', $andX);
$qb->orderBy('f.id', 'DESC');
$result = $qb->getQuery()->getResult();
return $result;
}
But nothing I tried worked. If only one value is sent, I get all the objects that has this value in their tags but as soon as there are more, it only get me the ones that matches the last one in the $tags array.

returning all records with the same ID but different values

I've been trying this query
Model::find()
->innerJoin('TranslationTable', 'TranslationTable.model_id = Model.id')
->where(['IN', 'translation_code', $arrayOfTranslationCodes])
->asArray()
->all();
The translation table contains multiple rows with the same ID but with different translation codes.
This query only returns the first matching locale for a given ID. How would I retrieve the other translation codes for a given ID?
This is the solution that I came to:
$query = Model::find()
-> innerJoin('TranslationTable', 'TranslationTable.model_id = Model.id');
foreach ($arrayOfTranslationCodes as $translation)
{
$query->andWhere(['OR', 'translation_code', $translation])
}
$queryResponse = $query->asArray()->all();
This allowed me to find rows with the same id, but have different translations. You need to store the $query->asArray()->all(); as $query itself just returns the active query.

Laravel 5: How to verify if values separated by comma from a database table field are contained in an array?

So, basically I have a field in a table from my database which contains some values separated by comma (something like: value1, value2, value3). I need to check somehow if one or more of these values are contained in an array of values and retrieve the model(s). I'd like a solution using Eloquent Model Query to achieve this, if not possible with Model Query, then a Query Builder solution will be okay as well, or maybe something alternative.
Tried to use a function inside the where statement where I loop the array of values and just query the model using like operator to check if field value matches the values from the array and in the end I got something like this:
$devices = explode(",", Input::get("devices"));
$products = Product::where(function ($q) use ($devices) {
foreach ($devices as $device) {
$q->orWhere("compatible_devices", "like", "%" . $device . "%");
}
})->get();
Works quite well so far.

Categories