<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                [TOC] # 更改 ~~~ $user=User::find(1); //取出主鍵等于1的記錄 $user->user_name='jdxia'; $user->update(); //更新記錄 $bool=Student::where('id','>',10)->update( ['age'=>41] ); ~~~ 用attributes ~~~ $post = Post::find(1); $raw = $post->getAttributes(); $post->setRawAttributes($raw); $post->save(); ~~~ ## 批量修改 ~~~ User::where('nickname', 'LIKE', '%man%')->update([ 'nickname' => 'Spider Man', ]); ~~~ > 這種方法不不會觸發updating、updated、saving、saved事件 # 查詢 這是在控制器寫,注意命名空間 ~~~ //all 取出所有 $nm=Student::all(); //get 取出所有 $nm=Student::get(); //findOrFail 根據主鍵查詢,查找到了返回結果,查不到就報錯 $nm=Student::findorFail(3); //連貫操作 $nm=Student::where('id','>','2') ->orderBy('age','desc')->first(); //chunk $nm=Student::chunk(2,function($students){ print_r($students); }); //聚合函數 $nm=Student::where('id','>',2)->max('age'); //findMany 根據ID數組查找多行數據 $user = User::findMany([1,2,3,4]); foreach ($users as $user) echo $user->id; ~~~ 一些日期 ~~~ $archives = Post::selectRaw('year(created_at) year, monthname(created_at) month, count(*) published')->groupBy('year','month')->orderByRaw('min(created_at) desc')->get(); $posts = Post::latest(); if ($month = request('month')) { $posts->whereMonth('created_at',Carbon::parse($month)->month); } if ($year = request('year')) { $posts->whereYear('created_at',$year); } $posts = $posts->get(); ~~~ 這里使用了 Laravel 提供的 whereDate 系列方法,同時,月份用 Carbon 進行轉換 ## take offset取幾條數據 ~~~ //take 取10條數句 $users = User::where('nickname', '=', 'nike')->orderBy('created', 'DESC')->take(10)->get(); //從第20行開始 $users = User::where('nickname', '=', 'nike')->orderBy('created', 'DESC')->take(10)->offset(20)->get(); ~~~ **first 只取一條,并得到Model(非數據集)** 通過first可以將數據提取出首條并返回 `$user = User::where('id', '=', 1)->first();` 查詢數據庫有沒有這條記錄 ~~~ //檢查用戶名是否存在 $user_exists=$this->where('username','=',$username) ->exists(); ~~~ # ORM中添加,自定義時間戳及批量賦值 通過模型新增數據(涉及到自定義事件戳) 使用模型的create方法新增數據(涉及到批量賦值) ## 添加 ~~~ //使用模型新增數據 $student=new Student(); $student->name='sean'; $student->age=18; $bool=$student->save(); //返回布爾值 //上面會自動維護created_at和updated_at,不想用可以關掉 ~~~ 如果我們想存儲時間戳呢?記住字段的類型不要搞錯 在模型中寫 ~~~ protected $table='stu'; //如果主鍵不是id,指定主鍵 protected $primarykey='id'; protected function getDateFormat() { return time(); } ~~~ 如果查詢的時候想要時間戳怎么辦 模型中寫 ~~~ protected function asDateTime($val) { return $val; } ~~~ ## create新增數據 批量添加數據,要在模型中寫 ~~~ //默認表示模型的復數 protected $table='stu'; //如果主鍵不是id,指定主鍵 protected $primarykey='id'; //指定允許批量賦值的字段 protected $fillable=['name','age']; //或者我們設置個這個guarded,就是不允許填充的字段為空數組[],這樣就可以填充進去了,就不用寫fillable了 protected $guarded=[]; ~~~ 這樣就可以用create批量添加的方法 ~~~ $student=Student::create( ['name'=>'jdxia','age'=>18] ); ~~~ ~~~ //以屬性查找用戶,有就查找,沒有就新增并取得新增的實例 $student=Student::firstOrCreate( ['name'=>'jdxia'] ); //firstOrNew() //以屬性查找用戶,有就查找,沒有就顯示實例,如需保存需自行調用save $student=Student::firstOrNew( ['name'=>'php'] ); $bool=$student->save(); ~~~ # 刪除 通過模型刪除 通過主鍵值刪除 根據指定條件刪除 ~~~ //通過模型刪除數據 $stu=Student::find(12); $bool=$stu->delete(); //通過主鍵刪除 //單個刪除 $bool=Student::destroy(11); //多個刪除 $bool=Student::destroy(9,10); $bool=Student::destroy([5,6]); //根據指定條件刪除 $bool=Student::where('id','>',3)->delete(); ~~~ ## 批量刪除 `User::where('nickname', 'LIKE', '%man%')->delete();` >這種方法不不會觸發deleting、deleted事件 ## 軟刪除 SoftDeletes DB類會查出軟刪除 ~~~ //SQL 庫中添加此字段 `deleted_at` timestamp NULL DEFAULT NULL, //Model 中添加 use Illuminate\Database\Eloquent\SoftDeletes; class User { use SoftDeletes; } ~~~ 此時,執行刪除操作(delete destory)操作時,并非真正刪除數據,只是在deleted_at字段中做了刪除標記 此時通過find get where update等操作來獲取數據時,會自動忽略這些軟刪除數據 ## 強制刪除 如需要強制刪除數據 ~~~ $user = User::find(1); $user->forceDelete(); ~~~ ## 查詢結果附帶軟刪除的數據 此時數據集中會附帶這些已被軟刪除的數據,后續操作時,需要自己甄別 `User::withTrashed()->where('nickname', 'Leo')->get();` ## 只返回軟刪除數據 `User::onlyTrashed()->where('nickname', 'Leo')->get();` ## 恢復軟刪除數據 ~~~ $user = User::onlyTrashed()->find(12); $user->restore(); ~~~ # 銷毀結果集 ~~~ $a=xx::where('id','=',12)->firstorFail(); $a->destory(); ~~~ # 遞增和遞減 平時這么寫: ~~~ $article = Article::find($article_id); $article->read_count++; $article->save(); ~~~ 利用 increment 函數 ~~~ $article = Article::find($article_id); $article->increment('read_count'); ~~~ 當然可以傳入數字,不只是只增減 1: ~~~ Article::find($article_id)->increment('read_count'); Article::find($article_id)->increment('read_count', 10); // +10 Product::find($produce_id)->decrement('stock'); // -1 ~~~ # WhereX 這里的 where 是前綴的作用,X表示的是我們的字段名,可以簡化我們的查詢寫法,平時都是這么寫的: ~~~ $users = User::where('approved', 1)->get(); ~~~ 簡便的寫法: ~~~ $users = User::whereApproved(1)->get(); ~~~ 具體實現主要利用 __call 方法。 public mixed __call ( string $name , array $arguments ) public static mixed __callStatic ( string $name , array $arguments ) 在對象中調用一個不可訪問方法時,__call() 會被調用。 在靜態上下文中調用一個不可訪問方法時,__callStatic() 會被調用。 在 Query/Builder.php 中可以看出: ~~~ /** * Handle dynamic method calls into the method. * * @param string $method * @param array $parameters * @return mixed * * @throws \BadMethodCallException */ public function __call($method, $parameters) { if (static::hasMacro($method)) { return $this->macroCall($method, $parameters); } if (Str::startsWith($method, 'where')) { return $this->dynamicWhere($method, $parameters); } $className = static::class; throw new BadMethodCallException("Call to undefined method {$className}::{$method}()"); } ~~~ where 查詢方法都會調用函數: ~~~ return $this->dynamicWhere($method, $parameters); ~~~ ~~~ /** * Handles dynamic "where" clauses to the query. * * @param string $method * @param string $parameters * @return $this */ public function dynamicWhere($method, $parameters) { $finder = substr($method, 5); $segments = preg_split( '/(And|Or)(?=[A-Z])/', $finder, -1, PREG_SPLIT_DELIM_CAPTURE ); // The connector variable will determine which connector will be used for the // query condition. We will change it as we come across new boolean values // in the dynamic method strings, which could contain a number of these. $connector = 'and'; $index = 0; foreach ($segments as $segment) { // If the segment is not a boolean connector, we can assume it is a column's name // and we will add it to the query as a new constraint as a where clause, then // we can keep iterating through the dynamic method string's segments again. if ($segment !== 'And' && $segment !== 'Or') { $this->addDynamic($segment, $connector, $parameters, $index); $index++; } // Otherwise, we will store the connector so we know how the next where clause we // find in the query should be connected to the previous ones, meaning we will // have the proper boolean connector to connect the next where clause found. else { $connector = $segment; } } return $this; } ~~~ 繼續看 addDynamic 函數: ~~~ /** * Add a single dynamic where clause statement to the query. * * @param string $segment * @param string $connector * @param array $parameters * @param int $index * @return void */ protected function addDynamic($segment, $connector, $parameters, $index) { // Once we have parsed out the columns and formatted the boolean operators we // are ready to add it to this query as a where clause just like any other // clause on the query. Then we'll increment the parameter index values. $bool = strtolower($connector); $this->where(Str::snake($segment), '=', $parameters[$index], $bool); } ~~~ 最后回到了` $this->where(Str::snake($segment), '=', $parameters[$index], $bool); `常規的 where 語句上; 同時,這過程我們可以發現 whereX 方法,不僅可以傳入一個字段,而且還可以傳入多個字段,用「And」或者 「Or」連接,且字段首字母用大寫「A~Z」。 # XorY methods 在平時有太多的寫法都是,先查詢,再判斷是否存在,然后再決定是輸出,還是創建。 如: ~~~ $user = User::where('email', $email)->first(); if (!$user) { User::create([ 'email' => $email ]); } ~~~ 一行代碼解決: ~~~ $user = User::firstOrCreate(['email' => $email]); ~~~ firstOrCreate 方法將會使用指定的字段 => 值對,來嘗試尋找數據庫中的記錄。如果在數據庫中找不到,`5.3`以下版本會使用屬性來添加一條記錄,`5.3`及以上版本則將使用第一個參數中的屬性以及可選的第二個參數中的屬性插入記錄 用法: ~~~php User::firstOrCreate(['name' => 'Lisi']); User::firstOrCreate(['name' => 'Lisi'], ['age' => 20]); // 5.3及以上版本支持 ~~~ # find() find() 函數通過主鍵獲取數據,平時都是獲取單數據,其實傳入的參數還可以是「主鍵數組」,獲取多 models。 ~~~ $users = User::find([1,2,3]); ~~~ 我們查看它的函數實現: ~~~ /** * Find a model by its primary key. * * @param mixed $id * @param array $columns * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection|static[]|static|null */ public function find($id, $columns = ['*']) { if (is_array($id) || $id instanceof Arrayable) { return $this->findMany($id, $columns); } return $this->whereKey($id)->first($columns); } ~~~ 首先判斷的是 id 是不是 array,如果是的話,則執行 findMany 函數: ~~~ /** * Find multiple models by their primary keys. * * @param \Illuminate\Contracts\Support\Arrayable|array $ids * @param array $columns * @return \Illuminate\Database\Eloquent\Collection */ public function findMany($ids, $columns = ['*']) { if (empty($ids)) { return $this->model->newCollection(); } return $this->whereKey($ids)->get($columns); } ~~~ 獲取的結果是一個 Collection 類型。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看