laravel 笔记
  • Introduction
  • 说明
  • Laravel 基础
    • 安装与配置
    • 目录结构
    • 路由配置
    • MVC 配置
    • 数据库操作
      • DB façade 方式
      • 查询构造器方式
      • Eloquent ORM 方式
      • 数据库迁移
      • 数据填充
    • 请求和响应和重定向
    • Session
    • 中间件
    • 其他
      • Artisan 命令行
  • Laraval 源码分析
    • 请求到响应的生命周期
      • 程序启动准备
      • 请求实例化
      • 处理请求
      • 响应的发送与程序终止
    • 路由
    • 补充知识点
      • 反射机制
      • Closure 类
  • 资料
  • 核心思想
    • 服务容器
    • 服务提供者
    • Facades(门脸模式)
  • 开发笔记
  • Artisan 命令
  • yarn
Powered by GitBook
On this page
  • 数据库操作(Eloquent ORM 方式)
  • 配置访问路由
  • 控制器
  • 模型

Was this helpful?

  1. Laravel 基础
  2. 数据库操作

Eloquent ORM 方式

Previous查询构造器方式Next数据库迁移

Last updated 5 years ago

Was this helpful?

参考:

数据库操作(Eloquent ORM 方式)

配置访问路由

routes/web.php文件中配置

Route::any('orm1', ['uses' => 'StudentController@orm1']);
Route::any('orm2', ['uses' => 'StudentController@orm2']);
Route::any('orm3', ['uses' => 'StudentController@orm3']);
Route::any('orm4', ['uses' => 'StudentController@orm4']);

控制器

文件示例App\Http\Controllers\StudentController

<?php

namespace App\Http\Controllers;

use App\Student;

class StudentController extends Controller
{
    public function ormQuery()
    {
        // all()
//        $students = Student::all();

        // find()
//        $student = Student::find(1001);

        // findOrFail()
//        $student = Student::findOrFail(1006);

        //var_dump($student);


//        $students = Student::get();
//        $student = Student::where('id', '>', '1001')
//            ->orderBy('age', 'desc')
//            ->first();
//        dd($student);

//        echo '<pre>';
//        Student::chunk(2, function($students) {
//            var_dump($students);
//        });

        // 聚合函数
        //$num = Student::count();
        $max = Student::where('id', '>', 1001)->max('age');
        var_dump($max);


    }

    public function ormAdd()
    {
        // 使用模型新增数据
//        $student = new Student();
//        $student->name = 'sean2';
//        $student->age = 20;
//        $bool = $student->save();
//        dd($bool);

//        $student = Student::find(1017);
//        echo date('Y-m-d H:i:s', $student->created_at);

        // 使用模型的Create方法新增数据

//        $student = Student::create(
//            ['name' => 'imooc', 'age' => 18]
//        );
//        dd($student);

        // firstOrCreate()
//        $student = Student::firstOrCreate(
//            ['name' => 'imoocs']
//        );

        // firstOrNew()
        $student = Student::firstOrNew(
            ['name' => 'imoocsss']
        );
        $bool = $student->save();
        dd($bool);

    }

    public function ormUpdate()
    {
        // 通过模型更新数据
//        $student = Student::find(1021);
//        $student->name = 'kitty';
//        $bool = $student->save();
//        var_dump($bool);

        $num = Student::where('id', '>', 1019)->update(
            ['age' => 41]
        );
        var_dump($num);
    }

    public function ormDelete()
    {

        // 通过模型删除
//        $student = Student::find(1021);
//        $bool = $student->delete();
//        var_dump($bool);

        // 通过主键删除
        //$num = Student::destroy(1020);
        //$num = Student::destroy(1018, 1019);
//        $num = Student::destroy([1016, 1017]);
//        var_dump($num);

        $num = Student::where('id', '>', 1004)->delete();
        var_dump($num);


    }
}

模型

文件示例App\Http\Student

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Student extends Model
{
    // 指定表名
    protected $table = 'student';

    // 指定id
    protected $primaryKey = 'id';

    // 指定允许批量赋值的字段(批量更新的时候用)
    protected $fillable = ['name', 'age'];

    // 指定不允许批量赋值的字段
    protected $guarded = [];


    // 自动维护时间戳
    public $timestamps = true;

    // 存入数据库时
    protected function getDateFormat()
    {
        return time();
    }

    // 获取时间时
    protected function asDateTime($val)
    {
        return $val;
    }
}
Eloquent: 入门