如果给定的模型实例已被软删除,则将类应用于yajra / datatable中的行

时间:2016-04-24 13:19:18

标签: php laravel datatable

yajra/laravel-datatables中,我想确定当前模型是否已被软删除。如果将其删除, function profilePage() { $CWID = $this->session->userdata('id'); $userType = $this->session->userdata('user_type'); $data = array('view' => 'viewProfile', 'userid' => $CWID, 'usertype' => $userType, 'user_info' => $this->Users_model->profileInfo($CWID, $userType)); $this->load->view('admin', $data); } 引导类将应用于包含该模型详细信息的行。

这是我的后端代码:

    <?php if($usertype =='advisor'): ?>
    <table>
        <?php foreach ($user_info as $row): ?>
        <tr>    
            <th>Name:  </th>
            <td><?php echo $row->user_fullname?></td>
        </tr>       
        <tr>
            <th>CWID: </th>
            <td><?php echo $row->CWID ?></td>
        </tr>
        <tr>
            <th>User Name: </th>
            <td><?php echo $row->user_name ?></td>
        </tr>
        <tr>
            <th>Email: </th>
            <td><?php echo $row->user_email ?></td>
        </tr>
        <tr>
            <th>Phone: </th>
            <td><?php echo $row->user_phone ?></td>
        </tr>
        <tr>
            <th>Major: </th>
            <td><?php echo $row->major ?></td>
        </tr>
        <tr>
            <th>Office Location: </th>
            <td><?php echo $row->office_loc ?></td>
        </tr>
        <?php endforeach ?>
    </table>    

<!--Provides view for a user that is an advisee-->
<?php elseif($usertype =='advisee'): ?>
    <table> 
    <?php foreach ($user_info as $row): ?>
        <tr>
            <th>Name: </th>
            <td><?php echo $row->user_fullname ?></td>
        </tr>
        <tr>
            <th>CWID: </th>
            <td><?php echo $row->CWID ?></td>
        </tr>
        <tr>
            <th>User Name: </th>
            <td><?php echo $row->user_name ?></td>
        </tr>
        <tr>
            <th>Email: </th>
            <td><?php echo $row->user_email ?></td>
        </tr>
        <tr>
            <th>Phone: </th>
            <td><?php echo $row->user_phone ?></td>
        </tr>
        <tr>
            <th>Major: </th>
            <td><?php echo $row->major ?></td>
        </tr>
        <tr>
            <th>Classification: </th>
            <td><?php echo $row->classification ?></td>
        </tr>
        <?php endforeach ?>
    </table>
    <!--Provides view for all other user types-->
<?php else: ?>
    <table> 
    <?php foreach ($user_info as $row): ?>
        <tr>
            <th>Name: </th>
            <td><?php echo $row->user_fullname ?></td>
        </tr>
        <tr>
            <th>CWID: </th>
            <td><?php echo $row->CWID ?></td>
        </tr>
        <tr>
            <th>User Name: </th>
            <td><?php echo $row->user_name ?></td>
        </tr>
        <tr>
            <th>Email: </th>
            <td><?php echo $row->user_email ?></td>
        </tr>
        <tr>
            <th>Phone: </th>
            <td><?php echo $row->user_phone ?></td>
        </tr>
        <?php endforeach ?>
    </table>
<?php endif ?>  

如你所见:

success

应用所需的类但是$courses = Course::select(['course_id', 'title', 'start_date', 'end_date', 'picture', 'lesson_count', 'status', 'active', 'teacher','start_date','end_date','reg_start_date','reg_end_date']); if ($request->has('showDeleted') && $request->get('showDeleted') == 1) { $courses = $courses->withTrashed(); } $datatable = app('datatables')->of($courses) ->orderBy('created_at', 'desc') //columns come here ->setRowClass(function ($course) { return ($course->trashed() ? 'danger' : 'sdasd'); }); return $datatable->make(true); 总是为所有模型实例返回false,即使这些实例没有被删除。

什么是最佳解决方案?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。 我错误地认为我应该在deleted_at方法的列选择中包含select字段:

$courses =
Course::select(['course_id', 'title', 'start_date', 'end_date', 'picture', 'lesson_count', 'status', 'active', 'teacher','start_date','end_date','reg_start_date','reg_end_date','deleted_at']);