table 软删除
Soft delete on table
我刚开始使用软删除,但我不太确定如何操作,我只是按照某人的示例操作,但仍然不确定如何进行正确的软删除。我只想删除 personal_info table 但我一直收到一条无错误消息,这让我迷路了,因为我不知道自己做错了什么,有人可以帮助我吗?非常感谢
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>
控制器:
public function delete($id = 0){
if ($id > 0){
personal_info::destroy($id);
}
return redirect("/home");
}
还是我应该这样做?
public function delete($id){
$data = personal_info::find($id)->delete();
return redirect("/home");
}
personal_info 型号:
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)
Route::get('/home/{id}/delete', 'HomeController@delete');
方法上不需要使用delete。尝试将您的功能更改为此
public function delete($id){
personal_info::findOrFail($id)->delete();
return back();
}
Edit
Route方法和Form方法必须相同
改变
Route::get('/home/{id}/delete', 'HomeController@delete');
到
Route::post('/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>
// 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>
我刚开始使用软删除,但我不太确定如何操作,我只是按照某人的示例操作,但仍然不确定如何进行正确的软删除。我只想删除 personal_info table 但我一直收到一条无错误消息,这让我迷路了,因为我不知道自己做错了什么,有人可以帮助我吗?非常感谢
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>
控制器:
public function delete($id = 0){
if ($id > 0){
personal_info::destroy($id);
}
return redirect("/home");
}
还是我应该这样做?
public function delete($id){
$data = personal_info::find($id)->delete();
return redirect("/home");
}
personal_info 型号:
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)
Route::get('/home/{id}/delete', 'HomeController@delete');
方法上不需要使用delete。尝试将您的功能更改为此
public function delete($id){
personal_info::findOrFail($id)->delete();
return back();
}
Edit
Route方法和Form方法必须相同
改变
Route::get('/home/{id}/delete', 'HomeController@delete');
到
Route::post('/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>
// 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>