【问题标题】:Laravel - Cannot Un Delete and Display Data using Soft DeleteLaravel - 无法使用软删除取消删除和显示数据
【发布时间】:2018-11-30 15:24:24
【问题描述】:

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

控制器

   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>  

网络上 在这里你可以看到我是如何调用我的控制器和查看页面的。

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;

【问题讨论】:

  • 添加您的模型配置,也许它缺少 softdeletes 特征。顺便说一句,是否显示任何错误?
  • @HCK 我已经添加了我的模型,请看一下编辑后的代码,谢谢
  • $clients = Client::onlyTrashed(); .这将只检索您已删除的行。

标签: php mysql laravel laravel-5 eloquent


【解决方案1】:

你能用-&gt;get()方法试试吗?

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

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

如果你想恢复被删除的数据,你必须使用-&gt;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'); 

控制器动作:

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

控制器动作:

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

$client-&gt;id 是客户端集合,我想你想在列出的 foreach 行中处于非活动状态,对吗?

【讨论】:

  • 好的,我尝试了get 方法,但即使它没有被删除,它也会获取所有数据。我只想在一行中显示我删除的数据
  • 对于唯一删除的数据,您必须使用 onlyTrashed() 而不是 withTrashed()。
  • 我欠你一个徽章先生,谢谢!我很感激你的回答:)
  • 现在如何恢复已删除的数据并再次恢复?最终输出?你能分享代码和逻辑吗?谢谢今天完成我的工作
  • 如果我想取消删除我的数据并再次返回,我该怎么做?
猜你喜欢
  • 2016-03-27
  • 2017-12-15
  • 2018-04-10
  • 2016-03-05
  • 1970-01-01
  • 2015-04-07
  • 1970-01-01
  • 1970-01-01
  • 2017-07-29
相关资源
最近更新 更多