1

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.

3
  • Can you show what you are getting in $allItems and which version of laravel you are using ? Commented Mar 4, 2016 at 10:45
  • $allItems are the rows retrieved from the table, including the soft deleted ones, that is working fine. I don't think the problem is laravel, the condition is working fine, the unset isn't. I'm using laravel 5.2. Commented Mar 4, 2016 at 10:48
  • unset(end($allRows)['actions']['delete']); changed it as $lastRow = end($allRows) unset($lastRow['actions']['delete']); Commented Mar 4, 2016 at 11:21

3 Answers 3

1

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,
        ),
    );
}
Sign up to request clarification or add additional context in comments.

1 Comment

I know several ways to achive this i just wanted to know why this way isn't working. This Anwser wouldn't work because it gives undefined index href on delete action.
0

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.

2 Comments

Well some rows have the delete actions, just the rows soft deleted ( $item->trashed(), which returns a bool ) haven't
Try to see which items have been removed temporarily echo $item->trashed(); Maybe Laravel not "know" recognize. I can not see errors in the code, so I assume that will be the Framework.
0

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']);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.