Laravel Eloquent Collection Search always returns false - database

When I use the Laravel Collection search function it always returns false.
Code :
$entries = Entry::all();
$results = $entries->search('Jack', true);
dd($results);
Output :false
Output dd($entries) :
Collection {#218 ▼
#items: array:9 [▼
0 => Entry {#219 ▼
#fillable: array:2 [▶]
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:5 [▶]
#original: array:5 [▶]
#casts: []
#dates: []
#dateFormat: null
#appends: []
#events: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
1 => Entry {#220 ▶}
2 => Entry {#221 ▶}
3 => Entry {#222 ▶}
4 => Entry {#223 ▶}
5 => Entry {#224 ▶}
6 => Entry {#225 ▶}
7 => Entry {#226 ▶}
8 => Entry {#227 ▶}
]
}
Sorry for the horrible formatting.

You need to use a simple collection with search() method. So, if you want to search for names, do this:
$entries = Entry::pluck('name');
$results = $entries->search('Jack');
BTW, this is a really bad way to do the search, because it will first load all entries into the memory and then will do the search.
A much better approach is to use Eloquent or Query Builder:
$results = Entry::where('name', 'like', '%'.$name.'%')->get();
Or simply:
$results = Entry::where('name', $name)->get();
When you want to search for a whole name.

You can always use the filter() function of Laravel Collections:
$matchingRecords = $allRecords->filter(function($key, $record){
return $record->name == "Jack";
});
Alternatives are the first() function to limit results to the first found, or search().
Check https://laravel.com/docs/5.4/collections#available-methods for more information.

Is there any value in your collection holding Jack ? and not f.e. 'jack' ?
According to the docs
https://laravel.com/docs/5.4/scout#searching
Entry::search('Jack')->get();
should do the trick, install the drivers tho.

I added the Eloquence[0] package.
[0] = https://github.com/jarektkaczyk/eloquence

Related

How to get unique value in multidimensional array in Laravel?

I have the following multidimensional array.
"PM" => array:6 [▼
0 => "Zeb"
1 => "Pen"
2 => "Zeb"
3 => "Eds"
4 => "Fsa"
5 => "Zeb"
]
"OS" => array:3 [▼
0 => "Min"
1 => "Kep"
2 => "Min"
]
"IT" => array:8 [▶]
]
And I would like to remove the duplicate values from the nested arrays. In this case, have only one value Zeb in PM and Min in OS. Please keep in mind that, I don't know in which array there are duplicates so I need a way to check all the nested arrays for duplicates.
Thank you.
$arr = [
"PM" => ["Zeb", "Pen", "Zeb", "Eds", "Fsa", "Zeb"],
"OS" => ["Min", "Kep", "Min"],
];
$cleanArray = collect($arr)->map(fn($subarr) => array_unique($subarr))->toArray();
will produce:
[
"PM" => ["Zeb", "Pen", "Eds", "Fsa"],
"OS" => ["Min", "Kep"]
]

How can you rename all the fields within a JSON array using Laravel that match X?

My array looks like this:
array:145 [▼
144 => array:2 [▼
0 => 1559739600000
1 => 39103.5828125
]
143 => array:2 [▼
0 => 1559739000000
1 => 39102.619270833
]
142 => array:2 [▼
0 => 1559738400000
1 => 39101.740234375
]
...
I need to change the 0s to time and 1s to data, so I can work with them within Laravel. If I try to reference them within Laravel like $0 I get T_Variable errors because of the rules that variables should not start with numbers. So I'm in a bind. So it ends up looking something like this:
array:145 [▼
144 => array:2 [▼
time => 1559739600000
data => 39103.5828125
]
143 => array:2 [▼
time => 1559739000000
data => 39102.619270833
]
142 => array:2 [▼
time => 1559738400000
data => 39101.740234375
]
...
You could do something like this using Laravel Collection.
$result = collect($array)->map(function ($item) {
return [
'time' => $item[0],
'data' => $item[1],
];
})->all();
I find this approach quite common in the Laravel community. For more info, pls check: https://laravel.com/docs/5.8/collections

How to turn get the value of json format to array

I am using laravel 5.
I query dates from this query:
$data = DB::table('itemregistrations')
->select('itemregistrations.dd', 'itemregistrations.mm', 'itemregistrations.yy')
->where('itemregistrations.itemregistrationid', $id)
->get();
I want to get the value to put in an array so that i can implode the array into string. I cannot implode it in this format.
dd($data);
produce this:
Collection {#521 ▼
#items: array:1 [▼
0 => {#515 ▼
+"dd": 15
+"mm": "0"
+"yy": 2007
}
]
}
How to turn this result to:
0 => {15, 0, 2007}
Thank you
why don't you convert the collection to an array using toArray() method and then simply do the json_encode on the array you got.
later on you can even pass it in your response.
Hope it helps other..
I find solution guided by those comments from this question here:
How do I implode a json object to a string in php?
I modify my code:
//query tarikh masuk
$datatarikh = DB::table('itemregistrations')
->select('itemregistrations.dd', 'itemregistrations.mm', 'itemregistrations.yy')
->where('itemregistrations.itemregistrationid', $id)
->get();
//**************** format the value of the date************************
$data = json_decode($datatarikh, true);
$val_arr = array();
foreach($data as $val)
{
foreach($val as $key => $value)
{
$val_arr[] = $value;
}
}
$result = implode("-", $val_arr);
It changes associative array to indexed array to be imploded. Thanks for your input.
From an array:
array:1 [▼
0 => array:3 [▼
"pdrm_dd" => 15
"pdrm_mm" => "01"
"pdrm_yy" => 2007
]
]
The output result:
"15-01-2007"

How can I iterate over an array of collections containing arrays in Laravel?

My collection is instantiated using the following query: $ClassesCollection = Classes::with('Queue')->whereIn('user_course_id', UserCourse::select('id')->where('user_id', $user->id))->get();
$ClassesCollection returns the desired result. However, I only want the Queue results.
So I split it this way
$classes = [];
foreach($ClassesCollection as $class){
array_push($classes, $class->Queue);
}
And now I have an array of collections, each holding arrays of Queues.
How can I iterate over these collections to display the content of each array of Queues separately in my view?
This is what $classes holds
array:10 [▼
0 => Collection {#968 ▼
#items: array:12 [▼
0 => Queue {#841 ▶}
1 => Queue {#842 ▶}
2 => Queue {#843 ▶}
3 => Queue {#844 ▶}
4 => Queue {#845 ▶}
5 => Queue {#846 ▶}
6 => Queue {#847 ▶}
7 => Queue {#848 ▶}
8 => Queue {#849 ▶}
9 => Queue {#850 ▶}
10 => Queue {#851 ▶}
11 => Queue {#852 ▶}
]
}
1 => Collection {#969 ▶}
2 => Collection {#970 ▶}
3 => Collection {#971 ▶}
4 => Collection {#972 ▶}
5 => Collection {#973 ▶}
6 => Collection {#974 ▶}
7 => Collection {#975 ▶}
8 => Collection {#976 ▶}
9 => Collection {#977 ▶}
]
And each Queue holds several attributes such as date and class_id. So what can I do to display the content of each Queue?
EDIT: I thought there would be a cleaner way but this is how I solved my problem for anyone who comes across this.
$count = 0;
foreach($classes as $collection => $queue){
/*$count = count($queue) - 1;
for($i = 0; $i <= $count; $i++){
echo $queue[$i]->date."\n";
}*/
foreach($queue as $Q){
echo $Q->date."\n";
}
}
The commented out code achieves the desired effect too.
Use pluck():
$queues = $ClassesCollection->pluck('Queue')->flatten();

Laravel 5 custom helper could not set session

I have custom helper, class, in my Laravel project:
<?php
namespace App\Http\Helpers;
class FoxUtils {
public static function isAuthTo($name)
{
if (self::test($name)){
\Session::push('AuthList',[$name => true]);
return true;
}
else{
\Session::push('AuthList',[$name => false]);
return false;
}
}
}
There two notes:
The session variable AuthList is numerical indexed array with values as arrays like the following:
array:2 [▼
0 => array:1 [▼
"name1" => true
]
1 => array:1 [▼
"newName" => true
]
]
The value "name1" => true is defined from other place than my helper. When I try to use my helper's method I respect new keys should be added to the array:
\FoxUtils::isAuthTo('AnotherName');
dd(session('AuthList'))
the above code prints array with only two keys while I expect three:
array:2 [▼
0 => array:1 [▼
"name1" => true
]
1 => array:1 [▼
"AnotherName" => true
]
]
In other words, always the last value of the AuthList is replaced with new value! What is the problem here?
From this I tried to use Session::save() and it worked fine:
....
if (self::test($name)){
\Session::push('AuthList',[$name => true]);
\Session::save();
return true;
}
....

Resources