php - Laravel - 无法使用软删除取消删除和显示数据

标签 php mysql laravel laravel-5 eloquent

您好,我无法删除我的数据,这是为什么?这是我的代码

Controller

   public function displayArchive()
{
    $clients = Client::withTrashed();
    return view('admin.clients.homeArchive')->with('clients', $clients);
}

其中一个查看

 <table class="table table-bordered" id="dynamic_field">  
           <tr>  
              <th>Client Code</th>
              <th>Client Name</th>
              <th>Address</th>
              <th>Tel No.</th>
              <th>Contact Person</th>
              <th>Mobile No.</th>
              <th>Email Address</th>
              <th>Website</th>
              <th>Status</th>
              <th>Update</th>

           </tr>  
           @foreach ($clients as $client)
           <tr>
               <td>{{ $client->client_code }}</td>
               <td>{{ $client->client_name }}</td>
               <td>{{ $client->address }}</td>
               <td>{{ $client->tel_no }}</td>
               <td>{{ $client->contact_person }}</td>
               <td>{{ $client->mobile_no }}</td>
               <td>{{ $client->email_ad }}</td>
               <td>{{ $client->website }}</td>
               <td><a href="#" class="btn btn-danger">Inactive</a></td>
               <td><a href="/admin/clients/{{ $client->id }}/edit" class="fa fa-edit btn btn-primary"></a></td>

           </tr>      
           @endforeach

        </table>  

网络上 在这里您可以看到我如何调用我的 Controller 和 View 页面。

Route::get('/admin/clients/homeArchive', 'Admin\ClientsController@displayArchive');

已编辑 这是编辑后的代码,请查看 我的模特

 use SoftDeletes;

 protected $dates = ['deleted_at'];
 // Table Name
 protected $table = 'clients';
 // Primary Key
 public $primaryKey = 'id';
 // Timestamps
 public $timestamps = true;

最佳答案

你可以尝试使用 ->get() 方法吗?

public function displayArchive()
{
    $clients = Client::withTrashed()->get();
    return view('admin.clients.homeArchive')->with('clients', $clients);
}

注意:它不会取消删除任何数据。它只获取已删除行的数据。

如果要恢复已删除的数据,必须使用->restore()方法。

<小时/>

对于所有恢复的所有数据;

链接:

<a href="{{ route('admin.client.restore_all') }}" class="btn btn-danger">Inactive</a>

路线:

Route::get('/admin/clients/restore-all', 'Admin\ClientsController@restoreAll')->name('admin.client.restore_all'); 

Controller 操作:

public function restoreAll(){
   Client::withTrashed()->restore();
}

逐行恢复数据;

链接:

<a href="{{ route('admin.client.restore', $client->id) }}" class="btn btn-danger">Inactive</a>

路线:

Route::get('/admin/clients/restore/{client}', 'Admin\ClientsController@restore')->name('admin.client.restore');

Controller 操作:

public function restore(Client $client){
  $client->restore();
}

$client->id 是客户端集合,我想你想在列出的 foreach 行中处于非事件状态,对吗?

关于php - Laravel - 无法使用软删除取消删除和显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53560333/

相关文章:

mysql - 使用 mysqldump 和数据库用户

php - 在 Laravel 中为现有数据库中的存储过程、函数和事件创建迁移

php - 如何在 Laravel 5.2 中使用 cookie

javascript - 使用对象将 javascript 变量传递给 php

php - Dokan 插件为单个订单的客户发送多封电子邮件

php - 使用数据库驱动数据在 MS Excel 中创建动态下拉菜单

php - Laravel 更新要求在保存到数据库之前填写所有输入

javascript - Laravel,将 javascript 导入 Blade 模板的正确方法

php - 动态输入表单php

php - 在 MySQL 中存储列表的正确方法是什么?