美文网首页
laravel ROM查询数据库

laravel ROM查询数据库

作者: 9a4a58bf4d80 | 来源:发表于2019-05-23 10:27 被阅读0次

    1.杂七杂八

    在模型中添加使用
    protected $guarded = [];      让所有属性都可以批量赋值 
    protected $guarded = ['price'];    设置属性黑名单
    -------------------------------------------------------------------------------------------------------------------------------
    $sgin=Sign::all();    查询全部数据库
    
    $aa->fill(['diastolic' => 'Flight 22']);    给查出来的对象添充属性
    
    $aa=Clock::where('user_id', 3)->update(['diastolic' => 0]);     批量更新 update()
    
    $flight->restore();    恢复软删除
    
    $flight->forceDelete();   强制删除模型实例
    

    2.查询

    $sgin=Sign::where('team_id',16)->take(2)->get();      限制查询多少条用 take() 
     
    $flight = Sign::find(1);      按ID查询   可是数组  查出来的也是数组  find()
    
    $flight = App\Flight::where('active', 1)->first();   查询符合条件的第一条 first()
    
    $flights = Sign::findOrFail(4);     查询是否有值 如果有值返回值, 如果没有值返回404
    
    $flights = Sign::where('user_id', '>', 1)->firstOrFail();    查询是否有值 如果有值返回值, 如果没有值返回404  firstOrFail()
    
    $count = Sign::where('community_id', 21)->count();     查询有多少条    count()
    
    $count = Sign::where('community_id', 21)->min('user_id');   查询某个字段的最小值
    
    $count = Sign::where('community_id', 21)->max('user_id');   查询某个字段的最大值
    
    $count = Sign::where('community_id', 21)->sum('user_id');   查询某个字段的总和
    

    3.插入和更新 save()

    单独使用是插入
         $flight = new Clock();
         $flight->user_id = 1;
         $flight->diastolic= 2;
         $flight->systolic=3;
         $flight->save();
    -------------------------------------------------------------------------------------------------------------------------------
    查询后是修改
         $flight = Clock::all();
         $flight->user_id = 2;
         $flight->save();
    -------------------------------------------------------------------------------------------------------------------------------
     用foreach 是插入第一条之后都是修改第一条
            $flight = new Clock();
            foreach ($aa as $value){
                $flight->user_id = $value;
                $flight->diastolic= 2;
                $flight->systolic=3;
                $flight->save();
            }
    
    1. 插入
    create 插入
          $aa = Clock::create(['user_id' => '1']);     
    -------------------------------------------------------------------------------------------------------------------------------
    save插入
         $flight = new Clock();                  
         $flight->user_id = 1;
         $flight->diastolic= 2;
         $flight->systolic=3;
         $flight->save();
    -------------------------------------------------------------------------------------------------------------------------------
    firstOrCreate  插入     如果数据有相同的就查出来,没有就创建一条新的记录
        $aa = Clock::firstOrCreate(['user_id' => '3']);
    -------------------------------------------------------------------------------------------------------------------------------
    
        
    

    5.修改

    updateOrCreate   修改
          $aa = Clock::updateOrCreate(
               ['id' => 21],
               ['user_id' => 6,'systolic' => 6,'diastolic' => 3]
           );
    save     修改
    $flight = Clock::all();
        $flight->user_id = 2;
        $flight->save();
    

    6.删除

    查询删除
        $flight = App\Flight::find(1);
        $flight->delete();
    主键删除
    App\Flight::destroy(1);
    App\Flight::destroy([1, 2, 3]);       主键删除可以放数组
    条件删除
    $deletedRows = App\Flight::where('active', 0)->delete();
    软删除   在模型里加
    use Illuminate\Database\Eloquent\SoftDeletes;
    use SoftDeletes;
    

    相关文章

      网友评论

          本文标题:laravel ROM查询数据库

          本文链接:https://www.haomeiwen.com/subject/edayzqtx.html