Eloquent基礎(chǔ)用法

Eloquent基礎(chǔ)用法

一、創(chuàng)建usermodel

執(zhí)行命令:

 php artisan make:model user

二、app目錄下出現(xiàn)User.php

<?php

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $primaryKey='user_id';//設(shè)置主鍵為"user_id",默認(rèn)為"id"
    protected $guarded = ['user_id'];//設(shè)置user_id字段不可以被重寫

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [//執(zhí)行查詢不會(huì)顯示的字段
        'password', 'remember_token',
    ];
    public function userTest(){
//        return $this->find(5);//查找主鍵為“5”的數(shù)據(jù),不存在返回空
//        return $this->findOrFail(5);//查找主鍵為“5”的數(shù)據(jù),不存在會(huì)報(bào)錯(cuò)
//        return $this->where('username','郭慶')->get();//查找用戶名為郭慶的數(shù)據(jù)
//        return $this->where('user_id','>','1')->get();//查找id大于1的數(shù)據(jù)

    }
}

三、在入口文件調(diào)用

Route::get('mysql',function(){
   $user=new App\User();
    return $user->userTest();
});

四、數(shù)據(jù)的增刪改

  1. 直接添加數(shù)據(jù)

     public function userAdd(){
         $this->username='lisi';
         $this->age=13;
         $this->save();
     }
    

    使用數(shù)組添加數(shù)據(jù)

      public function userAdd(){
           $data=['username'=>'撒媳婦','age'=>100];
           $this->fill($data);//填充數(shù)據(jù)
           $this->save();
       }
    
  2. 單條刪除:

     public function userDelete(){
           $users=$this->find(10);
           $users->delete();
     }
    

    批量刪除:

     public function userDelete(){
             $users=$this->where('age','<',30);
             $users->delete();
         }
    
  3. 單條修改:

     public function userUpdate(){
             $user=$this->find(5);
             $user->username='sb';
             $user->age=29;
             $user->save();
         }
    

    批量修改:

     public function userUpdate(){
             $users=$this->where('age','<',30);
             $users->update(['username'=>'小屁孩']);
         }
    

五、集合

$arr=['one'=>1,2,3,4,5];
$collection=collect($arr);
$r=$collection->contains('one');//判斷是否有這個(gè)值
$r=$collection->has('one');//判斷是否有這個(gè)鍵
$r=$collection->take(2);//取出前倆個(gè)值
$r=$collection->take(-2);//取出后倆個(gè)值    
return $r?'有':'沒(méi)有';

其余方法參見(jiàn)手冊(cè):
https://laravel.com/docs/5.3/collections

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,993評(píng)論 19 139
  • Ubuntu下數(shù)據(jù)庫(kù)操作命令 插入三條記錄到users表中 查詢表user中所有記錄 在項(xiàng)目根目錄創(chuàng)建一個(gè)mode...
    曹淵說(shuō)創(chuàng)業(yè)閱讀 192評(píng)論 0 0
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語(yǔ)法,類相關(guān)的語(yǔ)法,內(nèi)部類的語(yǔ)法,繼承相關(guān)的語(yǔ)法,異常的語(yǔ)法,線程的語(yǔ)...
    子非魚(yú)_t_閱讀 31,779評(píng)論 18 399
  • 配置 修改config/database.php在connection數(shù)組中添加mongodb的配置信息,如下 '...
    jooohnny閱讀 8,474評(píng)論 3 8
  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的閱讀 13,552評(píng)論 5 6