我正在尝试从 Laravel 5.3 中的 table 中删除特定记录
I am trying to delete a particular record from the table in Laravel 5.3
我的查看页面代码是
@foreach($clients as $client)
<tr>
<td>{{ $client->client_id }}</td>
<td>{{ $client->ip_address }}</td>
<td>{{ $client->netmask }}</td>
<td>
<a href= "del/{{$client->id}}"><button type = "button" class = "btn btn-danger ">
Delete
</button>
</td>
</tr>
@endforeach
我的销毁方法控制器代码是:
public function destroy($id)
{
$clients = Subnet_behind_client::findOrFail( $id );
$clients->delete();
return view('view2',compact('clients'));
}
我的路由文件是:
Route::get('del/{id}', 'Subnet_Behind_ClientController@destroy');
我可以在我的查看页面中查看 table 中的记录,但我无法从 table 中删除记录。
我觉得你的代码不错。
顺便说一下,如果您要使用主键删除记录,那么您可以使用 destroy()
https://laravel.com/docs/5.3/eloquent#deleting-models
Subnet_behind_client::destroy( $id );
这样您就不需要获取要删除的记录。它会稍微优化性能。
您正在尝试对从 findOrFail()
方法返回的记录集合调用 delete()
方法。
通过这样做,您无法访问查询生成器,因为现在您有一个 Collection
而不是 QueryBuilder
。
要使其正常工作,您可以这样做:
$clients = Subnet_behind_client::findOrFail( $id )->delete();
return view('view2',compact('clients'));
希望对您有所帮助!
请问您可以这样使用吗:
首先打印数据(如果存在)或检查现有条目。
$clients = Subnet_behind_client::where('id', $id)->get();
// dd($clients);
if(count($clients)>0){
Subnet_behind_client::where('id', $id)->delete();
}
我想这个技巧会对你有所帮助。
如果您忘记提及定义控制器顶部,请确保:
use App\Subnet_behind_client;
这应该有效:
Subnet_behind_client::destroy( $id );
对于删除,我建议使用 DELETE
请求而不是 GET
。
我的查看页面代码是
@foreach($clients as $client)
<tr>
<td>{{ $client->client_id }}</td>
<td>{{ $client->ip_address }}</td>
<td>{{ $client->netmask }}</td>
<td>
<a href= "del/{{$client->id}}"><button type = "button" class = "btn btn-danger ">
Delete
</button>
</td>
</tr>
@endforeach
我的销毁方法控制器代码是:
public function destroy($id)
{
$clients = Subnet_behind_client::findOrFail( $id );
$clients->delete();
return view('view2',compact('clients'));
}
我的路由文件是:
Route::get('del/{id}', 'Subnet_Behind_ClientController@destroy');
我可以在我的查看页面中查看 table 中的记录,但我无法从 table 中删除记录。
我觉得你的代码不错。
顺便说一下,如果您要使用主键删除记录,那么您可以使用 destroy()
https://laravel.com/docs/5.3/eloquent#deleting-models
Subnet_behind_client::destroy( $id );
这样您就不需要获取要删除的记录。它会稍微优化性能。
您正在尝试对从 findOrFail()
方法返回的记录集合调用 delete()
方法。
通过这样做,您无法访问查询生成器,因为现在您有一个 Collection
而不是 QueryBuilder
。
要使其正常工作,您可以这样做:
$clients = Subnet_behind_client::findOrFail( $id )->delete();
return view('view2',compact('clients'));
希望对您有所帮助!
请问您可以这样使用吗:
首先打印数据(如果存在)或检查现有条目。
$clients = Subnet_behind_client::where('id', $id)->get();
// dd($clients);
if(count($clients)>0){
Subnet_behind_client::where('id', $id)->delete();
}
我想这个技巧会对你有所帮助。
如果您忘记提及定义控制器顶部,请确保:
use App\Subnet_behind_client;
这应该有效:
Subnet_behind_client::destroy( $id );
对于删除,我建议使用 DELETE
请求而不是 GET
。