在Model里调用self::where等操作时,其实是先到了Model类的 __callStatic 方法,再到了 __call 方法,根据情况,再到了Builder文件,也就是:
vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php
大多数的操作都可以去这里面去找。
别的一些常用操作记录
$flight = App\Models\Flight::create(['name' => 'Flight 10']); $flight->fill(['name' => 'Flight 22']); Model::find(1, [字段]) ProductModel::where('id', '1')->value('id') ProductModel::where(['id' => '1'])->value('id') ProductModel::where(['id' => '1'])->get(['id','name'])->toArray() ProductModel::where(['id' => '1'])->first(['id','name'])->toarray() # find in set whereRaw("FIND_IN_SET(?, accept_type)", [$param['accept_type']]) // 直接取条一条记录 ProductModel::firstWhere("id", 1) 或 ProductModel::firstWhere("id", "=", 1) // Insert并获取主键 ProductModel::create(['name'=>'sdfsdfds'])->id ProductSkuPropertyDataModel::updateOrCreate( ['sku_id' => $skuId, 'property_id' => $property['id']], [ 'product_id' => $productId, 'sku_id' => $skuId, 'property_id' => $property['id'], 'property_value' => $pValue ] ); ProductStyleImageModel::whereIn('origin_hash', [2,3,4]) // insert $user = User::create([ 'first_name' => 'Taylor', 'last_name' => 'Otwell', 'title' => 'Developer', ]); // var_dump(ProductModel::where(['id' => '1'])->get(['id','name'])->toarray()); // var_dump(ProductModel::create(['name'=>'sdfsdfds'])->id); // 插入或更新,其实是先select判断再执行 $spiderStatModel = SpiderStatModel::subSite($site['id']); $spiderStatModel->updateOrCreate($conditions, $data); // 或者直接: Model::updateOrCreate($conditions, $array) //批量更新 App\Models\Flight::where('active', 1) ->where('destination', 'San Diego') ->update(['delayed' => 1]); //单条更新 $flight = App\Models\Flight::find(1); $flight->name = 'New Flight Name'; $flight->save(); $styles = DB::table("t_product_style_0 as product_style") ->where([['product_style.product_id', '=', $product->id]]) ->limit(1) ->get(); $model = self::from('huddle as huddle') ->leftJoin("store as store", "huddle.store_id", "=", "store.id") ->join("store_data as store_data", "huddle.store_id", "=", "store_data.store_id") ->join("store_site as store_site", "huddle.store_id", "=", "store_site.store_id") ->where([ 'huddle.topic_id' => $actId, 'huddle.status' => self::STATUS, 'store_data.status' => self::STATUS, 'store_site.status' => self::STATUS, 'store_site.site_id' => $siteId, 'store_data.lang_id' => $langId, ]) ->where('huddle.status', '>', time()) ->offset($page * $pageSize) ->limit($pageSize); $ret = $model->get([ 'huddle.id', 'huddle.store_id', 'store_data.name', 'huddle.rebate', 'huddle.places', 'huddle.start_time', 'huddle.end_time', 'store.is_upto' ]) ->toArray(); // firstOrNew 需要手动调用 save,才会保存到数据库。适合同时需要修改其他属性的场景。 // firstOrCreate 会自动保存到数据库。适合不需要额外修改其他属性的场景。 use Illuminate\Database\Query\Builder as QueryBuilder;