how to make select option value same as name in blades - laravel

DateTimeZone::listIdentifiers() returns array of timezones. Values are:
[
0 => "Africa/Abidjan",
1 => "Africa/Accra",
2 => "Africa/Addis_Ababa",
...
]
When I write {{ Form::select('timezone', DateTimeZone::listIdentifiers(), $result->timezone]) }} in blade, option value will be 0, 1, 2. I want to make option value equal to name of timezones

Just use array_combine()
Form::select('timezone', array_combine(DateTimeZone::listIdentifiers(), DateTimeZone::listIdentifiers()), $result->timezone])

Related

increment or decrement without using laravel methods mangodb jassengers

I want to update documents and decrement specific column value. it is possible to use Model::where('','')->update(['count' => \DB::raw('count- 1')]); i'm using jenssegers/laravel-mongodb package.
I want decrement and update documents values together.
$result= Source::where('')
->where('');
$result->decrement('count');
$result->update([
'column' => true,
]);
This code decrement count value but can not update column values.
From the docs:
Perform increments or decrements (default 1) on specified attributes:
Cat::where('name', 'Kitty')->increment('age');
Car::where('name', 'Toyota')->decrement('weight', 50);
i found it from laravel doc
updated column value pass 3rd parameter as array in decrement method
Model::where('', null)
->where('', '')
->decrement('count', 1, [
'column1' => value1,
'column2' => value2,
'column3' => value3,
]);

Laravel: Access Rules::in variable items

How to access the items listed in an Illuminate\Validation\Rules\In variable ?
use Illuminate\Validation\Rules\In;
$foo = Rule::in('a', 'b');
$foo->toString() // error
The only way I found to show it is:
>>> dump($foo)
Illuminate\Validation\Rules\In^ {#3512
#rule: "in"
#values: array:2 [
0 => "a"
1 => "b"
]
}
you can convert that Illuminate\Validation\Rules\In to collection then take the result array:
$foo = Rule::in('a', 'b');
$values = collect($foo)->values()[1];
to get the string Representation from it:
$stringRepresentation=$foo->__toString();

How to update all column with 0 value to specific value?

I want to update all column with 0 value to 1
Controller
$totalofitem=assets::where('item_name','like','keyboard')
->where('total_cost','like','0')->update(array('total_cost' => '1'));
Got it
$totalofitem=assets::where('item_name','like','keyboard')
->where('total_cost','like','0')->update(['total_cost' => 1]);

JSON is object instead of array, if array_diff returns assoc array on Collection->toArray()

My issue is in my json I am expecting an array, but am getting an object.
Details:
I have an array of numbers:
$numbers = [1];
I select from relationship, the "drawn numbers":
$drawnNumbers = Ball::whereIn('number', $numbers)->where('game_id', $card->game->id)->get()->map(function($ball) {
return $ball->number;
})->toArray();
I do a ->toArray() here. I want to find the numbers in $numbers that do not occur in $drawnNumbers. I do so like this:
$numbersNotYetDrawn = array_diff($numbers, $drawnNumbers);
My method then return $numbersNotYetDrawn (my headers accept is application/json).
So now the issue. When $drawnNumbers is an empty array, then the printed json is a regular array like this:
[
1
]
However if the relationship returns $drawnNumbers to be an array with numbers, then json is printed as an object:
{
"0" => 1
}
Does anyone know why this is? Anyway to ensure that json is array?
Edit:
Here is my actual data:
$drawnNumbers = Ball::whereIn('number', $numbers)->where('game_id', $card->game->id)->get()->map(function($ball) {
return $ball->number;
})->toArray();
$undrawnNumbers = array_diff($numbers, $drawnNumbers);
// $undrawnNumbers = array_values(array_diff($numbers, $drawnNumbers)); // temp fix
Replace
$numbersNotYetDrawn = array_diff($numbers, $drawnNumbers);
with
$numbersNotYetDrawn = array_values(array_diff($numbers, $drawnNumbers));
to make sure element keys are reset and array is treated as a simple list and serialized to a JSON list - instead of being treated as an associative array and serialized to a JSON object.
I recently had this same problem and wondered the same thing.
I solved it by adding "array_values", but I was wondering how to reproduce it.
I found it that it is reproduced when array_diff removes an element from the array that isn't the last element. So:
>>> $x
=> [
1,
2,
3,
4,
5,
]
>>> array_diff($x, [5]);
=> [
1,
2,
3,
4,
]
>>> array_diff($x, [1]);
=> [
1 => 2,
2 => 3,
3 => 4,
4 => 5,
]

How to get an array of 2-tuples from Laravel query builder?

Basically, I want to do this:
$locals['companies'] = Company::orderBy('name')->get(['id','name'])->map(function($c) { return [$c->id, $c->name]; })->toArray();
But without such a verbose map function. Isn't there a get-like method that will return flat numeric arrays instead of objects?
To be clear, the output should look like this:
array:4 [
0 => array:2 [
0 => 4
1 => "My Company"
]
1 => array:2 [
0 => 14
1 => "Example Company"
]
2 => array:2 [
0 => 13
1 => "Best Company"
]
3 => array:2 [
0 => 12
1 => "Super Co"
]
]
This is what I mean by 2-tuples: two-element numeric arrays. I know they don't exist in PHP, but the concept is the same; each entry has a fixed length.
There is no function out of the box to do this, but Laravel's Collection is Macroable, so you can add your own function to it to do this.
For example, somewhere in your code (like the boot() method of your AppServiceProvider), you can add a new method to the Collection:
// add toIndexedArray method to collections
\Illuminate\Support\Collection::macro('toIndexedArray', function() {
return array_map('array_values', $this->toArray());
});
Now you can use this new method like any other normal Collection method, so your final code would be:
$locals['companies'] = Company::orderBy('name')->get(['id','name'])->toIndexedArray();
If this is something you need a lot, you can change the PDO fetch mode in config/database.php to PDO::FETCH_NUM. I'm assuming it's possible to change it on-the-fly as well, but the code probably won't look that great. I don't think there's a Laravel command to change it for a single query, I'm afraid.
Otherwise, since the array is multidimensional, I'm afraid you do need to map over them somehow, and Laravel collections don't work nicely with e.g. ->map('array_values') which would have been a lot cleaner.
You could wrap it in array_map('array_values', $array) but that seems silly.
At least you could make it a little shorter if you change ->map() to ->transform() - then you don't need to tack on the ->toArray() at the end.
Use pluck():
$locals['companies'] = Company::orderBy('name')->pluck('id', 'name')->toArray();
If you need a list for Form::select this will work:
$locals['companies'] = Company::orderBy('name')->pluck('name', 'id');
You can omit the map function and just do:
$locals['companies'] = Company::orderBy('name')->get(['id','name'])->toArray();

Resources