Laravel 表上的软删除
我刚开始使用软删除,但我不确定该怎么做,我只是在跟随某人的例子,仍然不确定如何做一个正确的软删除。我只想删除personal_info表,但我一直收到一条无错误消息,这让我不知所措,因为我不知道自己做错了什么,有人能帮我吗?非常感谢 home.blade.phpLaravel 表上的软删除,laravel,soft-delete,Laravel,Soft Delete,我刚开始使用软删除,但我不确定该怎么做,我只是在跟随某人的例子,仍然不确定如何做一个正确的软删除。我只想删除personal_info表,但我一直收到一条无错误消息,这让我不知所措,因为我不知道自己做错了什么,有人能帮我吗?非常感谢 home.blade.php <table class="table table-bordered"> <tr> <th><strong><big>Name: </
<table class="table table-bordered">
<tr>
<th><strong><big>Name: </big></strong></th>
<th><strong><big>Action </big></strong></th>
</tr>
<td>
<tr>
@foreach($data as $value)
<tr>
<th><a href="{{route('user.show',['id'=>$value->id])}}">{{$value->Name}}</a></th>
<th><form action="{{ url('/home/'.$value->id.'/delete') }}" method="post">
<button>Delete</button>
</form></th>
</tr>
@endforeach
</tr>
</tr>
</table>
<table class="table table-bordered">
<tr>
<th><strong><big>Name: </big></strong></th>
<th><strong><big>Action </big></strong></th>
</tr>
<td>
<tr>
// Example of adjusting your query to support soft deletes
@php
$data = Some\Model::whereNotNull('deleted_at')->get();
@endphp
@foreach($data as $value)
<tr>
<th><a href="{{route('user.show',['id'=>$value->id])}}">{{$value->Name}}</a></th>
<th><form action="{{ url('/home/'.$value->id.'/delete') }}" method="post">
<button>Delete</button>
</form></th>
</tr>
@endforeach
</tr>
</tr>
</table>
还是我应该这样做
public function delete($id){
$data = personal_info::find($id)->delete();
return redirect("/home");
}
个人信息模型:
use Illuminate\Database\Eloquent\Model;
use Eloquent;
use SoftDeletes;
class personal_info extends Eloquent
{
protected $fillable = array('Name');
protected $table = 'personal_infos';
protected $primaryKey = 'id';
protected $dates = ['deleted_at'];
public function user_info1s() {
return $this->hasMany('App\user_info1','user_id');
}
路线:(不确定是否应改为使用删除)
无需在方法上使用delete。尝试将您的函数更改为此
public function delete($id){
personal_info::findOrFail($id)->delete();
return back();
}
编辑
路由方法和表单方法必须相同。更改
Route::get('/home/{id}/delete', 'HomeController@delete');
到
控制器方法
use Carbon\Carbon;
public function delete($id)
{
personal_info::find($id)->update([
'deleted_at' => Carbon::now()
]);
return redirect()->back();
}
home.blade.php
<table class="table table-bordered">
<tr>
<th><strong><big>Name: </big></strong></th>
<th><strong><big>Action </big></strong></th>
</tr>
<td>
<tr>
@foreach($data as $value)
<tr>
<th><a href="{{route('user.show',['id'=>$value->id])}}">{{$value->Name}}</a></th>
<th><form action="{{ url('/home/'.$value->id.'/delete') }}" method="post">
<button>Delete</button>
</form></th>
</tr>
@endforeach
</tr>
</tr>
</table>
<table class="table table-bordered">
<tr>
<th><strong><big>Name: </big></strong></th>
<th><strong><big>Action </big></strong></th>
</tr>
<td>
<tr>
// Example of adjusting your query to support soft deletes
@php
$data = Some\Model::whereNotNull('deleted_at')->get();
@endphp
@foreach($data as $value)
<tr>
<th><a href="{{route('user.show',['id'=>$value->id])}}">{{$value->Name}}</a></th>
<th><form action="{{ url('/home/'.$value->id.'/delete') }}" method="post">
<button>Delete</button>
</form></th>
</tr>
@endforeach
</tr>
</tr>
</table>
名称:
行动
//调整查询以支持软删除的示例
@php
$data=Some\Model::whereNotNull('deleted_at')->get();
@endphp
@foreach(数据为$value)
删去
@endforeach
使用软删除,将使用时间戳,因此基本上软删除数据库记录,您将给特定记录“deleted_at”字段一个时间戳,然后当您在前端循环数据时,您将使用类似“where delete_at==NULL”的内容调整查询,但如何给时间戳,在我接下来的参考资料中,我所做的只是这样做,“$table->softDeletes();”@WesMurrayI还使用了where子句“personal_info::findOrFail($id)->where('deleted_at','=',NULL)->delete();”并删除了我的所有数据@WesMurrayI无法理解为什么您要将user.show作为
显示,然后作为表单删除。。。为什么不将两者都作为一个
,然后路由将是route::get()
您能解释一下“use Carbon\Carbon;”是什么吗?或者这只是您使用过的一个示例吗?在控制器中使用“use Carbon\Carbon”启用Carbon,然后使用Carbon::now()和allocate timestaok nvm,我在里面添加了php标记
<table class="table table-bordered">
<tr>
<th><strong><big>Name: </big></strong></th>
<th><strong><big>Action </big></strong></th>
</tr>
<td>
<tr>
// Example of adjusting your query to support soft deletes
@php
$data = Some\Model::whereNotNull('deleted_at')->get();
@endphp
@foreach($data as $value)
<tr>
<th><a href="{{route('user.show',['id'=>$value->id])}}">{{$value->Name}}</a></th>
<th><form action="{{ url('/home/'.$value->id.'/delete') }}" method="post">
<button>Delete</button>
</form></th>
</tr>
@endforeach
</tr>
</tr>
</table>