Laravel框架Eloquent ORM刪除數(shù)據(jù)操作示例
本文實例講述了Laravel框架Eloquent ORM刪除數(shù)據(jù)操作。分享給大家供大家參考,具體如下:
這篇文章,以下三個知識點希望大家能夠掌握
如下:
- 通過模型刪除
- 通過主鍵值刪除
- 通過指定條件刪除
NO.1模型刪除
老樣子,我們先新建一個方法,然后輸入代碼。
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);
}
}
如果他顯示出了一個true,則證明刪除成功,如果沒有刪除成功,則報錯
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);
}
}
如果他輸出一個數(shù)字1,說明刪除成功,受影響的刪除數(shù)據(jù)總數(shù)為1,當(dāng)然,如果要刪除多條數(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);
}
}
效果如下:

這里說明我刪除了兩條數(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大于三的都會刪除,我就不手動演示了
更多關(guān)于Laravel相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Laravel框架入門與進(jìn)階教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于Laravel框架的PHP程序設(shè)計有所幫助。
相關(guān)文章
php設(shè)置session值和cookies的學(xué)習(xí)示例
一直沒弄懂Session,cookies什么的登陸驗證到底是怎么回事,昨天分別用HttpURLConnection和HttpClient兩個類來實驗了一下,基本弄明白了Session驗證登陸的機(jī)制和這兩個類的區(qū)別?,F(xiàn)在分享給大家2014-03-03
php設(shè)計模式之建造器模式分析【星際爭霸游戲案例】
這篇文章主要介紹了php設(shè)計模式之建造器模式,結(jié)合星際爭霸游戲案例形式分析了PHP建造器模式相關(guān)概念、原理、用法及操作注意事項,需要的朋友可以參考下2020-01-01

