I would apply a method for each element of an array to an object, how to achieve that ?
$array = ['state'=>'CA','name'=>'San Francisco']
//how to call ->where on each element to produce that ?
$documents = $this->reference
->where('state', '=', 'CA')
->where('name', '=', 'San Francisco');
->documents();
$query= $this->reference;
foreach($array as $field => $value) {
$query->where($field, '=', $value);
}
$documents = $query->documents();
The foreach syntax accepts a $k => $value pattern as its second parameter; for each element in the array, you execute the code in the foreach with $k being the current key of the array, and $v being its associated value.
Related
Given I have an simple non-associative array $values of SomeObject items indexed by 0, 1, 2, etc.
What is the best syntax to construct associative array $valuesByIndex indexed by some value extracted from original items?
What I constructed is:
$key = function($val) {
return $val->getSomeProperty();
};
$valuesByIndex = array_combine(array_map($key, $values), $values);
Which is pretty equals to:
$key = function($val) {
return $val->getSomeProperty();
};
$valuesByIndex = [];
foreach ($values as $val) {
$valuesByIndex[$key($val)] = $val;
}
Looking for pretty compact syntax.
In this piece:
$key = function($val) {
return $val->getSomeProperty();
};
$valuesByIndex = [];
foreach ($values as $val) {
$valuesByIndex[$key($val)] = $val;
}
We can simply replace $key($val) with the contents of the function $val->getSomeProperty() Which gives us
$valuesByIndex = [];
foreach ($values as $val) $valuesByIndex[$val->getSomeProperty()] = $val;
Because if the function returns this $val->getSomeProperty() we can just put it right in there and forgo that function completely.
However if this is from a DB call you may be able to use (in PDO) PDO::FETCH_GROUP which takes the first column of the Select and makes it the top level key.
I'm currently trying to implement a search in my db for keywords. Therefore I split a string seperated by a comma, so I get an array containing a variable number of elements (one keyword).
I know that I can use a eloquent construct like that:
$products = Product::where([['keywords', 'LIKE', %samsung%], [keywords, 'LIKE', 's7']])->paginate(15);
to find a product with the keywords samsung,galaxy,s7.
Now I need this thing but automatically generated for a variable number of search query parts, so for every keyword in the array I need to add one ['keywords', 'LIKE', '...']...
How can I do this with Laravels Eloquent?
Use closure. First, make sure you stored list of keywords into array or the like. Then ...
$keywords = ['samsung', 's7', 'what else'];
$products = Product::where(function ($query) use ($keywords) {
foreach ($keywords as $keyword) {
$query->orWhere('keyword', 'like', $keyword);
}
})->paginate(15);
other example
$keywords = [
['name', 'LIKE', $searchQuery],
['category_id', '=', $selectedSubcategory],
];
$products = Product::where(function ($query) use ($keywords) {
foreach ($keywords as $keyword) {
$query->where($keyword);
}
})->paginate(15);
other than other
$keywords = [
['name', 'LIKE', $searchQuery],
['category_id', '=', $selectedSubcategory],
['please_id', '=', $learnPhpArray],
];
$products = Product::query();
foreach ($keywords as $keyword) {
$products = $products->where($keyword);
}
return $products->paginate(15);
What does the orWhere do? Does it connect the query parts with a AND?
No, with OR. As the inverse(?) the where itself does AND by default.
References
https://laravel.com/api/5.2/Illuminate/Database/Eloquent/Builder.html#method_where
https://laravel.com/api/5.2/Illuminate/Database/Eloquent/Builder.html#method_orWhere
Isn't it just to generate the array? Or am I misunderstanding the question?
<?php
$keys = ['samsung', 'lg', 'sony', 'nokia', 'apple'];
$keywords = [];
foreach($keys as $key){
$keywords[] = ['keywords', 'LIKE', '%'.$key.'%'];
}
$products = Product::where($keywords)->paginate(15);
You can do this:
$query = "Samsung galaxy S7"; // Or whatever the query is
$words = preg_split("/[ ,.]/",$query); //Split on space comma or dot. Maybe need more?
$queries = array_map(function ($word) {
return [ "keywords", "LIKE", "%$word%" ];
}, $words); //Transform single words to array of queries
$products = Product::where($queries)->paginate(15);
Check how the first part of the code works at: https://eval.in/730772
laravel 5.6
$keyWords = KeyWordModel::all();
$keyWordQuerys = [];
foreach ($keyWords as $key => $item) {
$keyWordQuerys[$key] = ['title', 'like', '%'.$item->name.'%'];
}
$contents = ContentModel::query();
foreach ($keyWordQuerys as $key => $item) {
$contents = $contents->orwhere([$item]);
}
$contents = $contents->orderBy('pubDate', 'DESC')->get();
There are many matching credentials inside the table which are inserted to an foreach loop however I am getting the results only from the first foreach element. How could I fix that?
foreach($matches as $match)
{
$object = $match->object;
$sales->whereRaw("match (`object`) against (?)", array($object));
if($match->colourBase == '1')
{
$sales->where('colour', '=', 'CC');
$sales->orWhere('colour', '=', 'CC+DD');
if($match->maxPrice)
{
$sales->where('price', '<=', $match->maxPrice);
}
if($match->minPrice)
{
$sales->where('price', '>=', $match->minPrice);
}
}
}
$results = $sales->orderBy('sales.updated_at', 'asc')->get();
#update
This is how I get $matches
$matches = Match::where('PeopleID', '=', $id)->get();
If you want only one record
use first() function.
No need to use foreach. :)
$matches = Match::where('PeopleID', '=', $id)->first();
Simple question to which I don't have an answer.
How can I change my array from this:
[{"sku":"6"},{"buyers":"7"},{"base":"8"}]
to this:
[{"sku":"6","buyers":"7","base":"8"}]
I have three queries for three different database tables:
$sku = DB::table('mapiranje')->select(DB::raw('count(*) as sku'))
->where('mate_fk', '=', NULL)
->get();
$kupac = DB::table('mapkupci')->select(DB::raw('count(*) as buyers'))
->where('kupci_fk', '=', NULL)
->get();
$base = DB::table('dist_base')->select(DB::raw('count(*) as base'))
->where('base_fk', '=', NULL)
->get();
now each returns:
[{"sku":"6"}]
[{"buyers":"6"}]
[{"base":"6"}]
I have used merge_array to make a single array, but I get:
[{"sku":"6"},{"buyers":"7"},{"base":"8"}]
what I want is:
[{"sku":"6","buyers":"7", "base":"8"}]
Refactor your code according to right Laravel way:
$result = [
'sku' => DB::table('mapiranje')->whereNull('mate_fk')->count(),
'buyers' => DB::table('mapkupci')->whereNull('kupci_fk')->count(),
'base' => DB::table('dist_base')->whereNull('base_fk')->count()
];
$result = [];
foreach($input as $oneInputRow) {
$result[$oneInputRow[0]] = $oneInputRow[1];
}
$target = array();
$start = array(array("sku"=>"6"), "buyers"=>"7"), "base"=>"8"));
foreach($start as $sub){
foreach($sub as $key => $val){
$target[$key] = $val;
}
}
Not shure if laravel provides any special syntax, but just with php I'd do it as above.
Basicly you loop over the start-array. In that you loop over every array to get the key/val combination and put that into the target-array.
For the second loop there would be other ways if you only have one entry in every secondary array.
Please try below code
$dd = '[{"sku":"6"},{"buyers":"7"},{"base":"8"}]';
$data = json_decode($dd,true);
$result = array();
foreach($data as $key=>$value){
foreach($value as $key1=>$value1){
$result[$key1] = $value1;
}
}
echo json_encode($result); //this will print your required format result
I have an array called $zip_in_distance
Array ([0] => Array([zip] => 12345, [distance] => 12345)).
If I am going to print the $value[zip], it is correct. But I get an empty array back. When I don't use a foreach-loop and I manually do the query, it is working. What am I doing wrong?
$shop = array();
foreach ($zip_in_distance as $key => $value) {
$q = Doctrine_Query::create()
->from('market m')
->where('m.zip = ? ', $value['zip'])
->execute();
$shop[] = $q;
}
return $shop;
My template:
foreach ($shops as $key => $list) {
echo $key . $list['id'] . '<br>';
}
I do have more than on market per zipcode. Thanks in advance!
Craphunter
Why use a foreach at all?
Try something like this:
return MarketTable::getInstance()
->whereIn('m.zip', array_map(
function($element) {return $element['zip'];},
$shop
))
->execute();