溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

thinkphp5的model方法如何使用

發(fā)布時(shí)間:2022-12-08 10:43:33 來(lái)源:億速云 閱讀:333 作者:iii 欄目:編程語(yǔ)言

這篇文章主要介紹了thinkphp5的model方法如何使用的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇thinkphp5的model方法如何使用文章都會(huì)有所收獲,下面我們一起來(lái)看看吧。

thinkphp5 model的使用方法:1、通過(guò)“User::all(function($query){...}”查詢多條記錄;2、使用“User::select(function($query){...}”查詢;3、通過(guò)find()方法和get()方法查詢;4、使用column()方法查詢數(shù)據(jù)。

Thinkphp5.0 的使用模型Model查詢

一、查詢多條記錄

獲取多個(gè)數(shù)據(jù)可以使用:select()方法和all()方法。

示例一:使用all()方法。

        //(1)篩選條件使用閉包函數(shù)
        $res = User::all(function($query){
            $query->where('id','>',0)->field('id,name,email');
        });
        foreach($res as $val){
            dump($val->toArray());
        }
        //(2)篩選條件使用where()方法
        $res = User::where('id','>',0)->field('id,name,email')->all();
        //致命錯(cuò)誤: Call to undefined method app\index\controller\User::all()

示例二:使用select()方法。

        //(1)篩選條件使用where()
        $res = User::where('id','>',0)->field('id,name,email')->select();
        foreach($res as $val){
            dump($val->toArray());
        }
        //(2)篩選條件使用閉包函數(shù)
        $res = User::select(function($query){
            $query->where('id','>',0)->field('id,name,email');
        });
        foreach($res as $val){
            dump($val->toArray());
        }

1、注意結(jié)果格式:

外層是數(shù)組,里層包含多個(gè)查詢出來(lái)的對(duì)象

不能直接使用toArray(),需要遍歷

2、使用all()方法時(shí),不能使用where等方法。

二、查詢一條記錄

獲取多個(gè)數(shù)據(jù)可以使用:find()方法和get()方法。

示例一:使用find()方法。

      //(1)篩選條件使用閉包函數(shù)
        $res = User::find(function($query){
            $query->where('name','=','zhang san');
        });
        dump($res->toArray());
        //(2)篩選條件使用where
        $res = User::where('name','=','zhang san')->find();
        dump($res->toArray());

示例二:使用get()方法。

        //(1)篩選條件使用閉包函數(shù)
        $res = User::get(function($query){
            $query->where('name','=','zhang san');
        });
        dump($res->toArray());
        //(2)錯(cuò)誤方式:篩選條件使用where()方法,
        $res = User::where('name','=','zhang san')->get();
        dump($res->toArray());
        //報(bào)錯(cuò):method not exist:think\db\Query->get
        //get()方式只能使用閉包形式。

三、查詢一個(gè)字段

使用value()方法

$email = User::where('name','=','zhang san')->value('email');
dump($email);

四、查詢列數(shù)據(jù)

使用column()方法:

        //獲取name字段這一列
        $res = User::where('id','>',0)->column('name');
        dump($res);
        //獲取name字段這一列,并且以id字段作為索引
        $res = User::where('id','>',0)->column('id,name');
        dump($res);
        //獲取name字段這一列,并且以id字段作為索引
        $res = User::where('id','>',0)->column('name','id');
        dump($res);
        //獲取id,name,email字段這三列,并且以id字段作為索引
        $res = User::where('id','>',0)->column('name,email','id');
        dump($res);

關(guān)于“thinkphp5的model方法如何使用”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“thinkphp5的model方法如何使用”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI