如何在 laravel livewire 中按 bool 值排序数组
How to order an array by bool values in laravel livewire
我有一个数组,我想通过“is_available”键对该数组进行排序,(首先是 true)
我正在使用 laravel 8 和一个 livewire 组件
0 => array:12 [▼
"id" => 1
"name" => "치킨 ~The Chicken~"
"is_available" => true
"nearest_customer_distance" => 4905.4423678942
"customers" => array:26 [▶]
]
1 => array:12 [▼
"id" => 2
"name" => "混ぜるな危険"
"is_available" => false
"customers" => array:10 [▶]
]
2 => array:12 [▼
"id" => 3
"name" => "Oh! Bánh mì"
"is_available" => true
"customers" => array:8 [▶]
]
3 => array:12 [▼
"id" => 5
"name" => "CHIJIMI DEVIL"
"is_available" => false
"customers" => array:44 [▶]
]
]
我正在尝试这个
$newFranchiseList = $this->getFranchiseListActualPage();
$finalFranchiseList = array_merge($this->franchiseList, $newFranchiseList);
$finalFranchiseList = collect($finalFranchiseList)->sortBy('is_available')->reverse();
$this->franchiseList = $finalFranchiseList->toArray();
但在我看来我是通过 ID 获得订单的,这是我的看法
@forelse($franchiseList as $franchise)
@if($franchise['is_available'])
{{$franchise['name']}}
IS AVAILABLE
@else
{{$franchise['name']}}
NOT AVAILABLE
@endif
@empty
@endforelse
如果我执行 dump($this->franchiseList)
列表显示顺序为 is_available!
注意: $this->franchiseList
在组件中从来没有使用过,唯一要使用的是最后一行,如果我不使用这一行,则列表为空
组件数据collection进程
首先,在javascript中调用一个 livewire 监听器
window.livewire.emit('franchises:selectedCoords', JSON.stringify(selectedCoords));
然后被组件接收
public $selectedLatitude,
$selectedLongitude,
$perPage = 20,
$franchiseList = [],
$page = 1,
$totalFranchises = 0,
$isLoaded = false;
protected $listeners = [
'franchises:selectedCoords' => 'getCoords'
];
public function render()
{
return view('livewire.franchise-list');
}
public function getCoords($selectedCoords)
{
if ($selectedCoords) {
if (!empty($this->franchiseList)) {
$this->clearList();
}
$this->selectedLatitude = json_decode($selectedCoords, true)['lat'];
$this->selectedLongitude = json_decode($selectedCoords, true)['lng'];
}
$this->getFranchiseList();
}
public function getFranchiseList()
{
if ($this->selectedLatitude && $this->selectedLongitude) {
if (!$this->allFranchisesAreLoaded())
{
$newFranchiseList = $this->getFranchiseListActualPage();
$finalFranchiseList = array_merge($this->franchiseList, $newFranchiseList);
$this->franchiseList = collect($finalFranchiseList)->sortBy('is_available')->reverse()->toArray();
}
$this->isLoaded = true;
}
}
记住 $this->franchiseList
永远不会被覆盖,也就是说,它只被使用一次,在行 $this->franchiseList = collect($finalFranchiseList)->sortBy('is_available')->reverse()->toArray ();
中,如果没有被覆盖,它最终会成为一个空数组,所以有没有其他 $this->franchiseList
具有 blade 视图
显示的顺序
Livewire 将在每个请求上序列化 public 属性,以便它可以发送到前端并存储在 JavaScript.
中
现在更重要的是:JavaScript 将对对象进行自己的内部排序,这是您无法阻止的。所以要解决这个问题,你必须在排序后 re-key 你的数组,这样键现在按你排序的升序排列。
基本上,在调用 ->toArray()
之前,您需要做的就是对其调用 ->values()
,从而按照它们的顺序获取值,然后 PHP 将赋值它是从 0 开始的新密钥。
$this->franchiseList = collect($finalFranchiseList)->sortBy('is_available')->reverse()->values()->toArray();
例如,JavaScript、
中的这样一个对象
{
1: 'foo',
0: 'bar',
}
将按 JavaScript 这样的内部排序
{
0: 'bar',
1: 'foo',
}
你无法改变这一点。这就是为什么你需要 re-key 它。
我有一个数组,我想通过“is_available”键对该数组进行排序,(首先是 true) 我正在使用 laravel 8 和一个 livewire 组件
0 => array:12 [▼
"id" => 1
"name" => "치킨 ~The Chicken~"
"is_available" => true
"nearest_customer_distance" => 4905.4423678942
"customers" => array:26 [▶]
]
1 => array:12 [▼
"id" => 2
"name" => "混ぜるな危険"
"is_available" => false
"customers" => array:10 [▶]
]
2 => array:12 [▼
"id" => 3
"name" => "Oh! Bánh mì"
"is_available" => true
"customers" => array:8 [▶]
]
3 => array:12 [▼
"id" => 5
"name" => "CHIJIMI DEVIL"
"is_available" => false
"customers" => array:44 [▶]
]
]
我正在尝试这个
$newFranchiseList = $this->getFranchiseListActualPage();
$finalFranchiseList = array_merge($this->franchiseList, $newFranchiseList);
$finalFranchiseList = collect($finalFranchiseList)->sortBy('is_available')->reverse();
$this->franchiseList = $finalFranchiseList->toArray();
但在我看来我是通过 ID 获得订单的,这是我的看法
@forelse($franchiseList as $franchise)
@if($franchise['is_available'])
{{$franchise['name']}}
IS AVAILABLE
@else
{{$franchise['name']}}
NOT AVAILABLE
@endif
@empty
@endforelse
如果我执行 dump($this->franchiseList)
列表显示顺序为 is_available!
注意: $this->franchiseList
在组件中从来没有使用过,唯一要使用的是最后一行,如果我不使用这一行,则列表为空
组件数据collection进程
首先,在javascript中调用一个 livewire 监听器
window.livewire.emit('franchises:selectedCoords', JSON.stringify(selectedCoords));
然后被组件接收
public $selectedLatitude,
$selectedLongitude,
$perPage = 20,
$franchiseList = [],
$page = 1,
$totalFranchises = 0,
$isLoaded = false;
protected $listeners = [
'franchises:selectedCoords' => 'getCoords'
];
public function render()
{
return view('livewire.franchise-list');
}
public function getCoords($selectedCoords)
{
if ($selectedCoords) {
if (!empty($this->franchiseList)) {
$this->clearList();
}
$this->selectedLatitude = json_decode($selectedCoords, true)['lat'];
$this->selectedLongitude = json_decode($selectedCoords, true)['lng'];
}
$this->getFranchiseList();
}
public function getFranchiseList()
{
if ($this->selectedLatitude && $this->selectedLongitude) {
if (!$this->allFranchisesAreLoaded())
{
$newFranchiseList = $this->getFranchiseListActualPage();
$finalFranchiseList = array_merge($this->franchiseList, $newFranchiseList);
$this->franchiseList = collect($finalFranchiseList)->sortBy('is_available')->reverse()->toArray();
}
$this->isLoaded = true;
}
}
记住 $this->franchiseList
永远不会被覆盖,也就是说,它只被使用一次,在行 $this->franchiseList = collect($finalFranchiseList)->sortBy('is_available')->reverse()->toArray ();
中,如果没有被覆盖,它最终会成为一个空数组,所以有没有其他 $this->franchiseList
具有 blade 视图
Livewire 将在每个请求上序列化 public 属性,以便它可以发送到前端并存储在 JavaScript.
中现在更重要的是:JavaScript 将对对象进行自己的内部排序,这是您无法阻止的。所以要解决这个问题,你必须在排序后 re-key 你的数组,这样键现在按你排序的升序排列。
基本上,在调用 ->toArray()
之前,您需要做的就是对其调用 ->values()
,从而按照它们的顺序获取值,然后 PHP 将赋值它是从 0 开始的新密钥。
$this->franchiseList = collect($finalFranchiseList)->sortBy('is_available')->reverse()->values()->toArray();
例如,JavaScript、
中的这样一个对象{
1: 'foo',
0: 'bar',
}
将按 JavaScript 这样的内部排序
{
0: 'bar',
1: 'foo',
}
你无法改变这一点。这就是为什么你需要 re-key 它。