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;
}
....
Related
i have value array like this
^ array:2 [▼
0 => "random value1<tr>random string1</tr>random endvalue1"
1 => "random value2<tr>random string2</tr>random endvalue2"
)
]
how do i change the value of the dimensional array above to be like below
^ array:2 [▼
0 => "random string1"
1 => "random string2"
)
]
im trying using
$array = array_map(function($key) {
$exp = explode('<tr>', $tes);
//$exp1 = explode('</tr>', exp[1]); -from this line3 my explode cant access array
return exp;
}, $myarray);
dd($array);
my array in line3 become like this
^ array:2 [▼
0 => array:2 [▼
0 => "random value1"
1 => "random string1</tr>random endvalue1"
]
1 => array:2 [▼
0 => "random value2"
1 => "random string2</tr>random endvalue2"
]
)
]
how to improve conditions like this, or have another way that is better ?
$data = [
"random value1<tr>random string1</tr>random endvalue1",
"random value2<tr>random string2</tr>random endvalue2",
];
$trim = collect($data)
->map(function (string $value) {
preg_match('/(?<=<tr>)(.*)(?=<\/tr>)/', $value, $match);
return empty($match) ? $value : $match[1];
})
->toArray();
Could be done this way, using the framework collect method to map through the values & extract the string you want.
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
This is how I create my array fields:
public function index($slug, Request $request, UserPasswordEncoderInterface $passwordEncoder)
{
$page = $this->getDoctrine()->getRepository(Pages::class)->findOneBy(['slug'=>$slug]);
$fields = (array) $page;
return $this->render('mypage.html.twig', ['page' => $page, 'fields' => $fields]);
}
The output is:
array:3 [▼
"\x00App\Entity\Pages\x00id" => 3
"\x00App\Entity\Pages\x00name" => "cat"
"\x00App\Entity\Pages\x00color" => ""
]
But I actually need this result:
array:3 [▼
"id" => 3
"name" => "cat"
"color" => ""
]
According to the suggestions I made this change:
public function index($slug, Request $request, UserPasswordEncoderInterface $passwordEncoder)
{
$page = $this->getDoctrine()->getManager()->getRepository(Pages::class)->findOneBy(['slug'=>$slug]);
$fields = get_object_vars($page);
return $this->render('mypage.html.twig', ['page' => $page, 'fields' => $fields]);
}
But this outputs me an empty array.
You have two options:
1. Use Query::HYDRATE_ARRAY instead of findOneBy()
$query = $this->getDoctrine()
->getRepository(Pages:class)
->createQueryBuilder('p')
->getQuery();
$result = $query->getResult(Query::HYDRATE_ARRAY);
(stolen from this answer)
2. Use a Serializer
Use the Serializer Component or JMSSerializerBundle to serialize your entity object.
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
I met some difficulties when I was trying to get value from an array in my php blade.
It has rather clear structure (printed with dd function)
{{dd($attr)}}
array:4 [▼
"id" => "215"
"type" => "select"
"name" => "Status"
"value" => array:2 [▼
"pred" => array:3 [▼
0 => "Employed"
1 => "On vacation"
2 => "Dismissed"
]
"sel_val" => "0"
]
]
And when I want to get a value by key 'sel_val' or 'pred'
print_r($attr['value']['pred']);
it gives me Illegal string offset 'pred'
And it works nice in Controller. What should i do?
It gives that error because pred is also an array. you'll have to do $attr['value']['pred'][0] to get Employed, $attr['value']['pred'][1] to get On vacation, $attr['value']['pred'][2] to get Dismissed and $attr['value']['sel_val'] to get the value of sel_val which is 0 in this case. Hope this helps.
working fine when we send array in compact function in the controller
$record = array('id' => '215', 'type' => 'select', 'value' => array('pred' => array('0'=> 'Employed', '1' => 'On vacation', '2' => 'Dismissed'),'sel_val' => '0'));
return view('home', compact('record'));