protected $dateFormat = 'U';
const CREATED_AT = 'time1';
const UPDATED_AT = 'updated_at';
protected $connection = 'mysql1';
protected $attributes = [
'time1' => '2021-10-19',
];
User::query()->take(5)->get();
$user = User::find(2);
$userfresh = $user->fresh();
$user = User::all();
[
$user = $user->reject(function ($user){
return $user->name=='啊实打实的';
});
$user = User::query()->chunk(5,function ($users){
Log::info($users);
});
[2021-12-30 09:25:13] local.INFO: [{"id":2,"name":"123","email":"294932451@qq.com","email_verified_at":null,"created_at":"2021-12-30T08:48:49.000000Z","updated_at":"2021-12-30T08:57:21.000000Z","time1":"2021-10-19 00:00:00"},{"id":3,"name":"\u554a\u5b9e\u6253\u5b9e\u7684","email":"zggMeKTnNvoVvFQl@qq.com","email_verified_at":null,"created_at":"2021-12-30T08:51:48.000000Z","updated_at":"2021-12-30T08:51:48.000000Z","time1":"2021-10-19 00:00:00"},{"id":4,"name":"IvRaqWZXvLsCI93C","email":"dPhSw4oVNYlKtfSY@qq.com","email_verified_at":null,"created_at":"2021-12-30T08:52:01.000000Z","updated_at":"2021-12-30T08:52:01.000000Z","time1":"2021-10-19 00:00:00"},{"id":5,"name":"xkp1ncyO8qruAacc","email":"GBVt5I2LaaRU5Hwh@qq.com","email_verified_at":null,"created_at":"2021-12-30T08:52:01.000000Z","updated_at":"2021-12-30T08:52:01.000000Z","time1":"2021-10-19 00:00:00"},{"id":6,"name":"F7P6BOV00SXLexxf","email":"HwvnOdqbX0TAOxfw@qq.com","email_verified_at":null,"created_at":"2021-12-30T08:52:02.000000Z","updated_at":"2021-12-30T08:52:02.000000Z","time1":"2021-10-19 00:00:00"}]
[2021-12-30 09:25:13] local.INFO: [{"id":7,"name":"lNrdLtDB15CAoM8E","email":"xl6iDJcGDXANe5FL@qq.com","email_verified_at":null,"created_at":"2021-12-30T08:52:02.000000Z","updated_at":"2021-12-30T08:52:02.000000Z","time1":"2021-10-19 00:00:00"},{"id":8,"name":"lNkEdkOt69qB1bty","email":"g2i3SF1ORlOCzSc5@qq.com","email_verified_at":null,"created_at":"2021-12-30T08:52:03.000000Z","updated_at":"2021-12-30T08:52:03.000000Z","time1":"2021-10-19 00:00:00"},{"id":9,"name":"gXSjl81Z444bWwEG","email":"CxzP7CUHnbOskhGx@qq.com","email_verified_at":null,"created_at":"2021-12-30T08:52:03.000000Z","updated_at":"2021-12-30T08:52:03.000000Z","time1":"2021-10-19 00:00:00"},{"id":10,"name":"JNn1XURxiJUtHvCG","email":"GtQGZ4jzWcq6eV7Y@qq.com","email_verified_at":null,"created_at":"2021-12-30T08:52:04.000000Z","updated_at":"2021-12-30T08:52:04.000000Z","time1":"2021-10-19 00:00:00"},{"id":11,"name":"RJc3BGyDfB9i3Wqv","email":"kmTqRXyVXeKlC5VY@qq.com","email_verified_at":null,"created_at":"2021-12-30T08:52:04.000000Z","updated_at":"2021-12-30T08:52:04.000000Z","time1":"2021-10-19 00:00:00"}]
$user = User::query()->whereNull('email_verified_at')->chunkById(2,function ($users){
$res = $users->each->update(['password'=>'1234567']);
Log::info($res);
});
[2021-12-31 03:21:12] local.INFO: [{"id":2,"name":"123","email":"294932451@qq.com","email_verified_at":null,"password":"1234567","created_at":"2021-12-30T08:48:49.000000Z","updated_at":"2021-12-31T03:21:12.000000Z","time1":"2021-10-19 00:00:00"},{"id":3,"name":"\u554a\u5b9e\u6253\u5b9e\u7684","email":"zggMeKTnNvoVvFQl@qq.com","email_verified_at":null,"password":"1234567","created_at":"2021-12-30T08:51:48.000000Z","updated_at":"2021-12-31T03:21:12.000000Z","time1":"2021-10-19 00:00:00"}]
[2021-12-31 03:21:12] local.INFO: [{"id":4,"name":"IvRaqWZXvLsCI93C","email":"dPhSw4oVNYlKtfSY@qq.com","email_verified_at":null,"password":"1234567","created_at":"2021-12-30T08:52:01.000000Z","updated_at":"2021-12-31T03:21:12.000000Z","time1":"2021-10-19 00:00:00"},{"id":5,"name":"xkp1ncyO8qruAacc","email":"GBVt5I2LaaRU5Hwh@qq.com","email_verified_at":null,"password":"1234567","created_at":"2021-12-30T08:52:01.000000Z","updated_at":"2021-12-31T03:21:12.000000Z","time1":"2021-10-19 00:00:00"}]
$user = User::cursor();
foreach ($user as $v){
Log::info($v);
}
$users = User::cursor()->filter(function ($user) {
return $user->id > 20;
});
foreach ($users as $user){
Log::info($user);
}
selects 子查询基本使用
航班表 地区表 查询到达城市最晚的航班
$res = Destinations::addselect(['last_flight'=>Flights::select('name')->whereColumn('destination_id','destinations.id')->OrderByDesc('arrived_at')->limit(1)])->get();
子查询排序
$res = Destinations::OrderByDesc(Flights::select('arrived_at')->whereColumn('destination_id','destinations.id')->OrderByDesc('arrived_at')->limit(1))->get();
dd($res->toArray());
firstOr
在找不到数据的时候会执行回调内的方法,并返回结果
$model = Destinations::where('id', '>', 3)->firstOr(function () {
return Destinations::firstwhere('id',1);
});
dd($model);
未找到异常
$flight = Flights::findOrFail(50);
$flight1 = Flights::where('id', '>', 50)->firstOrFail();
dd($flight,$flight1);
检索或创建模型 firstOrCreate firstOrNew
$flight = Flights::firstOrCreate(
['name' => 'London to Paris'],
['arrived_at' => '2022-01-01 14:50:37']
);
$flight1 = Flights::firstOrNew(
['name' => 'London to beijing'],
['arrived_at' => '2022-05-01 14:50:37']
);
只会增加第一个,第二个需要save才会加入
$flight1->save();
|