namespace App\Http\Controllers;
use App\Student;
use Illuminate\Support\Facades\DB;
class StudentController extends Controller
{
public function orm1()
{
$students = Student::all();
dd($students);
}
}
显示数据库里的所有数据 2.find方式
代码如下:
namespace App\Http\Controllers;
use App\Student;
use Illuminate\Support\Facades\DB;
class StudentController extends Controller
{
public function orm1()
{
$students = Student::find(1);
dd($students);
}
}
查找指定数据 3.findOrFail方式
代码如下:
namespace App\Http\Controllers;
use App\Student;
use Illuminate\Support\Facades\DB;
class StudentController extends Controller
{
public function orm1()
{
$students = Student::findOrFail(1);
dd($students);
}
}
namespace App\Http\Controllers;
use App\Student;
use Illuminate\Support\Facades\DB;
class StudentController extends Controller
{
public function orm1()
{
$students = Student::get();
dd($students);
}
}
他会得到一个完整的数据信息,和原本的意义没有区别
2.first方式使用
代码如下:
namespace App\Http\Controllers;
use App\Student;
use Illuminate\Support\Facades\DB;
class StudentController extends Controller
{
public function orm1()
{
$student = Student::where('id','>',1)
->orderBy('age','desc')
->first();
dd($student);
}
}
当id大于一的时候,获取一个最大值的age
3.where方式使用
代码如下:
namespace App\Http\Controllers;
use App\Student;
use Illuminate\Support\Facades\DB;
class StudentController extends Controller
{
public function orm1()
{
$student = Student::where('id','>',1)
->get();
dd($student);
}
}
4.chunk方式使用
代码如下:
namespace App\Http\Controllers;
use App\Student;
use Illuminate\Support\Facades\DB;
class StudentController extends Controller
{
public function orm1()
{
$student = Student::chunck(2,function($student){
var_dump($student);
});
}
}
5.聚合函数的使用
1.count函数
代码如下:
namespace App\Http\Controllers;
use App\Student;
use Illuminate\Support\Facades\DB;
class StudentController extends Controller
{
public function orm1()
{
$student = Student::count();
dd($student);
}
}
2.max函数
代码如下:
namespace App\Http\Controllers;
use App\Student;
use Illuminate\Support\Facades\DB;
class StudentController extends Controller
{
public function orm1()
{
$student = Student::max('age');
dd($student);
}
}
3.min函数
代码如下:
namespace App\Http\Controllers;
use App\Student;
use Illuminate\Support\Facades\DB;
class StudentController extends Controller
{
public function orm1()
{
$student = Student::min('age');
dd($student);
}
}
4.avg函数
代码如下:
namespace App\Http\Controllers;
use App\Student;
use Illuminate\Support\Facades\DB;
class StudentController extends Controller
{
public function orm1()
{
$student = Student::avg('age');
dd($student);
}
}
5.sum函数
代码如下:
namespace App\Http\Controllers;
use App\Student;
use Illuminate\Support\Facades\DB;
class StudentController extends Controller
{
public function orm1()
{
$student = Student::sum('age');
dd($student);
}
}