2016-03-04 48 views
1

我从数据库中的表中检索所有行,以便将它们显示在数据表的客户端中,并使用数据和相应的操作。这是不相关的,但我使用laravel。这是我在做什么:在foreach循环中取消设置多维数组

$allItems = $model::withTrashed()->get(); 
$allRows = array(); 
foreach ($allItems as $item) { 

     $allRows[] = array(
      $item->id, 
      $item->name, 
      $item->category->name, 
      $item->url, 
      strftime('%d-%m-%Y %H:%M', $item->updated), 
      'actions' => array(
       'view' => array(
        'href' => $this->_mainTable. '/view/' .$item->id, 
       ), 
       'delete' => array(
        'href' => $this->_mainTable. '/delete/' .$item->id, 
        'modal' => array(
         'question' => 'Delete job:', 
         'name' => $item->name. '?', 
         'advice_phrase' => '', 
         'btn_confirm' => 'Delete', 
         'class_phrase' => '', 
        ), 
       ), 
      ), 
     ); 

     if($item->trashed()) { 
      unset(end($allRows)['actions']['delete']); 
      dd(end($allRows)['actions']); 
     } 
} 

它不给我任何错误,并在var_dum() - >(dd(end($allRows)['actions'])),里面如果它仍然显示的删除操作。我不明白为什么这不起作用。

+0

你能告诉你在'$ allItems'得到什么,你所使用的laravel的版本? –

+0

'$ allItems'是从表格中检索到的行,包括软删除的行,它工作正常。我认为这个问题并不严重,条件正常,没有解决。我正在使用laravel 5.2。 – Miguel

+0

'unset(end($ allRows)['actions'] ['delete']);'将其更改为 '$ lastRow = end($ allRows)unset($ lastRow ['actions'] ['delete']) ;' –

回答

1

请尝试这样。

$allItems = $model::withTrashed()->get(); 
$allRows = array(); 
foreach ($allItems as $item) { 
    $delete = array(); 
    if (!$item->trashed()) { 
     $delete = array(
      'action_table' => $this->_mainTable, 
      'href' => $this->_mainTable . '/view/' . $item->id, 
      'modal' => array(
       'question' => 'Delete job:', 
       'name' => $item->name . '?', 
       'advice_phrase' => '', 
       'btn_confirm' => 'Delete', 
       'class_phrase' => '', 
      ), 
     ); 
    } 

    $allRows[] = array(
     $item->id, 
     $item->name, 
     $item->category->name, 
     $item->url, 
     strftime('%d-%m-%Y %H:%M', $item->updated), 
     'actions' => array(
      'view' => array(
       'action_table' => $this->_mainTable, 
       'href' => $this->_mainTable . '/view/' . $item->id, 
      ), 
      'delete' => $delete, 
     ), 
    ); 
} 
+0

我知道几种方法来实现这一点,我只是想知道为什么这种方式不工作。这个Anwser不会工作,因为它在删除操作上给出了* undefined index href *。 – Miguel

0

让我们来看看我是否理解。你想只显示操作视图?如果是这样,请不要将操作删除添加到数组$ allItems。

+0

一些行有删除行为,只是行'soft deleted'('$ item-> trashed()',它返回一个bool)没有 – Miguel

+0

尝试看看哪些项目已被临时删除 'echo $ item-> trashed();' 也许Laravel不“知道”认出。 我看不到代码中的错误,所以我认为这将是框架。 –

+0

我已经找到解决方案了,我发布了我的anwser。谢谢 – Miguel

0

这个作用似乎是多余的,我已经能够实现这一目标:

$allItems = $model::withTrashed()->get(); 
$allRows = array(); 
foreach ($allItems as $key => $item) { 

    $allRows[] = array(
     $item->id, 
     $item->name, 
     $item->category->name, 
     $item->url, 
     strftime('%d-%m-%Y %H:%M', $item->updated), 
     'actions' => array(
      'view' => array(
       'href' => $this->_mainTable. '/view/' .$item->id, 
      ), 
      'delete' => array(
       'href' => $this->_mainTable. '/delete/' .$item->id, 
       'modal' => array(
        'question' => 'Delete job:', 
        'name' => $item->name. '?', 
        'advice_phrase' => '', 
        'btn_confirm' => 'Delete', 
        'class_phrase' => '', 
       ), 
      ), 
     ), 
    ); 

    if($item->trashed()) { 
     unset($allRows[$key]['actions']['delete']); 
     dd(end($allRows)['actions']); 
    } 
}