laravel模型的建立查询数据

Route::get('test3','PhotoController@test3');

这里写图片描述

//代码在PhotoController
 public function test3(){
        $res = PhotoModel::all();
        dd($res);//集合
    }

这里写图片描述

$res = PhotoModel::find(2);
        dd($res);//模型对象

这里写图片描述

        $res = PhotoModel::findOrFail('2');//2  99 没有会报错
        dd($res);//模型对象

这里写图片描述

  $res = PhotoModel::get();
        dd($res);//查询所有
$res = PhotoModel::where('Id','>','20')->orderBy('age','desc')->first();
dd($res);
PhotoModel::chunk(2,function($res){
            echo '<pre>';
            var_dump($res);
            echo '</pre>';
        });//一次查询两条数据
object(Illuminate\Database\Eloquent\Collection)#476 (1) {
  ["items":protected]=>
  array(2) {
    [0]=>
    object(App\PhotoModel)#473 (26) {
      ["table":protected]=>
      string(5) "photo"
      ["primaryKey":protected]=>
      string(2) "Id"
      ["connection":protected]=>
      string(5) "mysql"
      ["keyType":protected]=>
      string(3) "int"
      ["incrementing"]=>
      bool(true)
      ["with":protected]=>
      array(0) {
      }
      ["withCount":protected]=>
      array(0) {
      }
      ["perPage":protected]=>
      int(15)
      ["exists"]=>
      bool(true)
      ["wasRecentlyCreated"]=>
      bool(false)
      ["attributes":protected]=>
      array(5) {
        ["id"]=>
        int(1)
        ["src"]=>
        string(2) "as"
        ["age"]=>
        int(99)
        ["created_at"]=>
        string(19) "2018-08-20 03:44:49"
        ["updated_at"]=>
        string(19) "2018-08-20 03:44:49"
      }
      ["original":protected]=>
      array(5) {
        ["id"]=>
        int(1)
        ["src"]=>
        string(2) "as"
        ["age"]=>
        int(99)
        ["created_at"]=>
        string(19) "2018-08-20 03:44:49"
        ["updated_at"]=>
        string(19) "2018-08-20 03:44:49"
      }
      ["changes":protected]=>
      array(0) {
      }
      ["casts":protected]=>
      array(0) {
      }
      ["dates":protected]=>
      array(0) {
      }
      ["dateFormat":protected]=>
      NULL
      ["appends":protected]=>
      array(0) {
      }
      ["dispatchesEvents":protected]=>
      array(0) {
      }
      ["observables":protected]=>
      array(0) {
      }
      ["relations":protected]=>
      array(0) {
      }
      ["touches":protected]=>
      array(0) {
      }
      ["timestamps"]=>
      bool(true)
      ["hidden":protected]=>
      array(0) {
      }
      ["visible":protected]=>
      array(0) {
      }
      ["fillable":protected]=>
      array(0) {
      }
      ["guarded":protected]=>
      array(1) {
        [0]=>
        string(1) "*"
      }
    }
    [1]=>
    object(App\PhotoModel)#474 (26) {
      ["table":protected]=>
      string(5) "photo"
      ["primaryKey":protected]=>
      string(2) "Id"
      ["connection":protected]=>
      string(5) "mysql"
      ["keyType":protected]=>
      string(3) "int"
      ["incrementing"]=>
      bool(true)
      ["with":protected]=>
      array(0) {
      }
      ["withCount":protected]=>
      array(0) {
      }
      ["perPage":protected]=>
      int(15)
      ["exists"]=>
      bool(true)
      ["wasRecentlyCreated"]=>
      bool(false)
      ["attributes":protected]=>
      array(5) {
        ["id"]=>
        int(2)
        ["src"]=>
        string(10) "hm2FX3iRsl"
        ["age"]=>
        int(26)
        ["created_at"]=>
        string(19) "2018-08-20 03:44:49"
        ["updated_at"]=>
        string(19) "2018-08-20 03:44:49"
      }
      ["original":protected]=>
      array(5) {
        ["id"]=>
        int(2)
        ["src"]=>
        string(10) "hm2FX3iRsl"
        ["age"]=>
        int(26)
        ["created_at"]=>
        string(19) "2018-08-20 03:44:49"
        ["updated_at"]=>
        string(19) "2018-08-20 03:44:49"
      }
      ["changes":protected]=>
      array(0) {
      }
      ["casts":protected]=>
      array(0) {
      }
      ["dates":protected]=>
      array(0) {
      }
      ["dateFormat":protected]=>
      NULL
      ["appends":protected]=>
      array(0) {
      }
      ["dispatchesEvents":protected]=>
      array(0) {
      }
      ["observables":protected]=>
      array(0) {
      }
      ["relations":protected]=>
      array(0) {
      }
      ["touches":protected]=>
      array(0) {
      }
      ["timestamps"]=>
      bool(true)
      ["hidden":protected]=>
      array(0) {
      }
      ["visible":protected]=>
      array(0) {
      }
      ["fillable":protected]=>
      array(0) {
      }
      ["guarded":protected]=>
      array(1) {
        [0]=>
        string(1) "*"
      }
    }
  }
}
    public function test3(){
//        $res = PhotoModel::all();
//        dd($res);//集合

//        $res = PhotoModel::find(2);
//        dd($res);//模型对象

//        $res = PhotoModel::findOrFail('2');//2  99 没有会报错
//        dd($res);//模型对象

//        $res = PhotoModel::get();
//        dd($res);

//        $res = PhotoModel::where('Id','>','20')->orderBy('age','desc')->first();
//        dd($res);

//        PhotoModel::chunk(2,function($res){
//            echo '<pre>';
//            var_dump($res);
//            echo '</pre>';
//        });//一次查询两条数据

//        $res = PhotoModel::count();
//        var_dump($res);
//
//        $res = PhotoModel::max('age');
//        var_dump($res);
//
//        $res = PhotoModel::min('age');
//        var_dump($res);
//
//        $res = PhotoModel::sum('age');
//        var_dump($res);
//
//        $res = PhotoModel::avg('age');
//        var_dump($res);

    }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值