I'm retrieving all rows from a table in database to display them in a datatables client side, with the data and respective actions. It isn't relevant but i'm using laravel. This is what i am doing:
$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']);
}
}
It's isn't giving me any error, and on the var_dum()
-> ( dd(end($allRows)['actions'])
), inside the if it's still showing the delete action. I don't understand why this isn't working.
Try like this.
$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,
),
);
}
Let's see if I understand. Do you want to only show actions view? If so, do not add the action delete to the array $allItems.
This seens redundant,i've been able to achive this:
$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']);
}
}