本文實(shí)例講述了Laravel框架Eloquent ORM刪除數(shù)據(jù)操作。分享給大家供大家參考,具體如下:
這篇文章,以下三個(gè)知識(shí)點(diǎn)希望大家能夠掌握
如下:
- 通過模型刪除
- 通過主鍵值刪除
- 通過指定條件刪除
NO.1模型刪除
老樣子,我們先新建一個(gè)方法,然后輸入代碼。
namespace App\Http\Controllers;
use App\Student;
use Illuminate\Support\Facades\DB;
class StudentController extends Controller
{
public function orm4()
{
$student = Student::find(7);//找到id為7的
$bool = $student->delete();//刪除
var_dump($bool);
}
}
如果他顯示出了一個(gè)true,則證明刪除成功,如果沒有刪除成功,則報(bào)錯(cuò)
NO.2通過主鍵值刪除
代碼如下:
namespace App\Http\Controllers;
use App\Student;
use Illuminate\Support\Facades\DB;
class StudentController extends Controller
{
public function orm4()
{
$num = Student::destroy(7);
var_dump($num);
}
}
如果他輸出一個(gè)數(shù)字1,說明刪除成功,受影響的刪除數(shù)據(jù)總數(shù)為1,當(dāng)然,如果要?jiǎng)h除多條數(shù)據(jù)也很簡單,代碼如下:
namespace App\Http\Controllers;
use App\Student;
use Illuminate\Support\Facades\DB;
class StudentController extends Controller
{
public function orm2()
{
$num = Student::destroy(7,5);
var_dump($num);
}
}
效果如下:
![](http://img.jbzj.com/file_images/article/201912/2019123114439894.png?2019113114521)
這里說明我刪除了兩條數(shù)據(jù)
NO.3通過指定條件刪除
代碼如下:
namespace App\Http\Controllers;
use App\Student;
use Illuminate\Support\Facades\DB;
class StudentController extends Controller
{
public function orm2()
{
$num = Student::where('id','>',3)
->delete();
var_dump($num);
}
}
這里,id大于三的都會(huì)刪除,我就不手動(dòng)演示了
更多關(guān)于Laravel相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Laravel框架入門與進(jìn)階教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于Laravel框架的PHP程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:- Laravel5.7 Eloquent ORM快速入門詳解
- Laravel 5框架學(xué)習(xí)之Eloquent (laravel 的ORM)
- Laravel Eloquent ORM 實(shí)現(xiàn)查詢表中指定的字段
- Laravel Eloquent ORM 多條件查詢的例子
- laravel 解決Eloquent ORM的save方法無法插入數(shù)據(jù)的問題
- Laravel框架Eloquent ORM新增數(shù)據(jù)、自定義時(shí)間戳及批量賦值用法詳解
- laravel框架數(shù)據(jù)庫操作、查詢構(gòu)建器、Eloquent ORM操作實(shí)例分析
- laravel 數(shù)據(jù)遷移與 Eloquent ORM的實(shí)現(xiàn)方法
- Laravel框架Eloquent ORM簡介、模型建立及查詢數(shù)據(jù)操作詳解
- Laravel框架Eloquent ORM修改數(shù)據(jù)操作示例
- laravel5.6 框架操作數(shù)據(jù) Eloquent ORM用法示例