dougui5419 2016-07-24 20:42
浏览 74

如何从php中的对象获取数据

I have an object so i want to know how can get data from object.Object example is given below

return $response= PowerConsumption::groupBY('device_id')
            ->selectRaw('round(sum(unit),4) as yAxis,device_id')
           ->where('created_at',Carbon::now())->paginate(1);

So this eloquent query create given data,but i need current_page and please don't change my query.Only help me to figure out current_page value from this response.

{
total: 2,
per_page: 1,
current_page: 1,
last_page: 2,
next_page_url: "http://localhost:8000/graph/monthly/user_id/2?page=2",
prev_page_url: null,
from: 1,
to: 1,
data: [
{
yAxis: 222.8572,
device_id: 1
}
]
}

This data is store in $response variable and i need only current_page and last_page so how can i get. i'm trying this like

$response['current_page']; //it's return null
$response->current_page; //it's return error like
Undefined property: Illuminate\Pagination\LengthAwarePaginator::$current_page

Kindly help me in getting data

  • 写回答

1条回答 默认 最新

  • duan0414 2016-07-24 20:46
    关注

    Simply use PHP object-access notation to get any data you want from your object as shown below... however, notice that you have a Typo in your code $response->current_page not $resonse->current_page; as @Quỳnh Nguyễn noted in his comment above.

    <?php
    
        /*
        YOUR $response, AS A PHP OBJECT, COULD BE REPRESENTED WITH THE OBJECT VARIABLE; $response BELOW
        {
        total: 2,
        per_page: 1,
        current_page: 1,
        last_page: 2,
        next_page_url: "http://localhost:8000/user/user_id/2?page=2",
        prev_page_url: null,
        from: 1,
        to: 1,
       } 
       */
    
        $response                 = new stdClass();
        $response->total          = 2;
        $response->per_page       = 1;
        $response->current_page   = 1;
        $response->last_page      = 2;
        $response->next_page_url  = "http://localhost:8000/user/user_id/2?page=2";
        $response->next_page_url  = null;
        $response->from           = 1;
        $response->to             = 1;
    
        // NOW TO GET THE current_page, you could do:
        $response->current_page;
    
    It is probable that you are using Laravel... if so, this is what your controller might look like:
    <?php
    
        namespace App\Http\Controllers;
    
        use Illuminate\Http\Request;
    
        use App\Http\Requests;
    
        class PowerController extends Controller{
    
            public function power(){ 
                $response = PowerConsumption::groupBY('device_id')
                                            ->selectRaw('round(sum(unit),4) as yAxis,device_id')
                                            ->where('created_at',Carbon::now())->paginate(1);
                // YOU SHOULD RETURN VIEW INSIDE YOUR METHOD
                // AND PASS THE $response AS ARGUMENT...
    
                return view('your_view_name', compact($response));              
            }
        }
    
    Now in your view; you should be able to access your data much like any normally declared variable:
    //your_view_name.blade.php
    {{ $current_page }}
    {{ $next_page_url }}
    {{ $prev_page_url }}
    {{ $total }}
    
    评论

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教