我有一个数组,我想按"is_available"键(先为true)排序该数组

  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),列表显示的顺序是可用的!

note: $this->franchiseList从未在组件中使用,唯一要使用的行是最后一行,如果我不使用这一行,列表为空

component data collection process

首先,在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将在每次请求时序列化公共属性,以便将其发送到前端并存储在JavaScript中.

现在关键是:JavaScript将对对象进行自己的内部排序,这是您无法阻止的.为了解决这个问题,你必须在对数组进行排序后重新设置关键帧,这样关键帧现在就按照你的排序顺序升序排列.

基本上,在调用->toArray()之前,只需对其调用->values(),从而按顺序获取值,PHP将从0开始为其分配新键.

$this->franchiseList = collect($finalFranchiseList)->sortBy('is_available')->reverse()->values()->toArray();

例如,在JavaScript中这样的对象,

{
    1: 'foo',
    0: 'bar',
}

将按JavaScript进行内部排序,如下所示

{
    0: 'bar',
    1: 'foo',
}

你不能改变这一点.这就是为什么你需要重新设置关键点.

Laravel相关问答推荐

如何更改LIVE上的Filament占位符内容?

Laravel:如何从不断更新的Controller中分页数据

子目录中具有Vue实例的Laravel不起作用

Laravel 模型事件:delete() 不会从存储中删除文件

Laravel Livewire 组件属性在 div 类中为空

Laravel | 使用select查询

Laravel 从 5.1 升级到 5.2.0 错误

Laravel:使用 Input::old 时 textarea 不会重新填充

Laravel 5 Eloquent 范围连接并 Select 特定列

即使上传了文件,Laravel Input::hasFile('image') 也会返回 false

Laravel Session 总是改变 Laravel 5.4 中的每个刷新/请求

Laravel 5.2 中的正则表达式验证

Laravel 5 迁移:Schema::table 方法中不同类型的默认长度

Laravel 从现有模型生成迁移

如何在 Laravel 中实现自己的 Faker 提供程序

在 Laravel 5.4 中向多个抄送收件人发送Electron邮件

Laravel 存储链接不适用于生产

如何获取 Laravel 块的返回值?

Laravel 中的 isDirty() 是什么意思?

如何使 Laravel (Blade) 文本字段只读