【问题标题】:Trying to get property of non-object from blade view in laravel试图从 laravel 的刀片视图中获取非对象的属性
【发布时间】:2020-06-04 07:41:35
【问题描述】:

我尝试将我的分页数据显示到数据库...我的分页数据来自我已经解码的 json .....我能够传递给我的分页数据以查看但无法在表格中显示 …… 这是刀片视图中的错误

试图获取非对象的属性

我的结果分页

  LengthAwarePaginator {#4177 ▼
      #total: 1
      #lastPage: 1
      #items: Collection {#4169 ▼
        #items: array:1 [▼
          "data" => array:852 [▼
            0 => {#624 ▶}
            1 => {#623 ▶}
            2 => {#628 ▼
              +"ID": "cust1"
              +"PWD": "W6ph5Mm5Pz8GgiULbPgzG37mj9g="
              +"NICK_NAME": "CUST1"
              +"PWD_INVALID_HIT": "0"
              +"PWD_CREATE_DT": "2019/07/22 15:57:15"
              +"INSTITUTION_ID": "C01"
              +"ACL_ID": "L04"
              +"LOGIN_DT": "2019/07/22 15:57:15"
              +"LOGIN_STS_ID": "N"
              +"STS_ID": "C08"
              +"TYPE_ID": "U00"
              +"UPD_ID": "sufyzasukor"
              +"UPD_DT": "2019/07/22 15:57:15"
              +"EMAIL_ID": "cust1@gmail.com"
              +"PHONE_NO_ID": "0"
              +"HP_ID": "0                        "
              +"CRT_DATE_DELETED": null
              +"irel__com_access_level": {#621 …9}
              +"irel__com_user_types": {#630 …5}
              +"irel__com_status": {#631 …5}
            }
}
  #perPage: "15"
  #currentPage: 1
  #path: "http://localhost/IFICSV3/public/configuration/comuserprofiles"
  #query: []
  #fragment: null
  #pageName: "page"
  +onEachSide: 3
  #options: array:2 [▶]
}

我的控制器

public function index()
    {


        $response =  $this->client->get('getUserIndex')->getBody();
         $content = json_decode($response->getContents());



         $total = count($content) ;
         $collection = new \Illuminate\Support\Collection($content);
         $paginationRecord = CollectionPaginate::paginate($collection, $total, '15');



         return view('configuration.comuserprofiles.ComUserProfilesList', ['paginationRecord' => $paginationRecord]);

}

我要显示的刀片表

    <tbody>
             @foreach($paginationRecord as $i=>$user)
              @php
              $currentRecordno = 1;

              @endphp  


              <tr>

       <td>{{ $user->ID }}</td>
       <td>{{ $user->NICK_NAME }}</td>
       <td>{{ $user->PWD_INVALID_HIT }}</td>


        </tr>
@endforeach 

dd($user)

array:852 [▼
  0 => array:20 [▶]
  1 => array:20 [▶]
  2 => array:20 [▼
    "ID" => "cust1"
    "PWD" => "W6ph5Mm5Pz8GgiULbPgzG37mj9g="
    "NICK_NAME" => "CUST1"
    "PWD_INVALID_HIT" => "0"
    "PWD_CREATE_DT" => "2019/07/22 15:57:15"
    "INSTITUTION_ID" => "C01"
    "ACL_ID" => "L04"
    "LOGIN_DT" => "2019/07/22 15:57:15"
    "LOGIN_STS_ID" => "N"
    "STS_ID" => "C08"
    "TYPE_ID" => "U00"
    "UPD_ID" => "sufyzasukor"
    "UPD_DT" => "2019/07/22 15:57:15"
    "EMAIL_ID" => "cust1@gmail.com"
    "PHONE_NO_ID" => "0"
    "HP_ID" => "0                        "
    "CRT_DATE_DELETED" => null
    "irel__com_access_level" => array:9 [ …9]
    "irel__com_user_types" => array:5 [ …5]
    "irel__com_status" => array:5 [ …5]

如何在表格中显示数据

【问题讨论】:

  • 什么是CollectionPaginate?我不认识。
  • @waterloomatt 它来自 App\Helpers\CollectionPaginate;
  • 尝试一个简单的:- $users = DB::table('users')-&gt;paginate(15); 了解更多信息:- laravel.com/docs/5.8/pagination

标签: php json laravel pagination


【解决方案1】:

您正在尝试将数组作为对象访问。这就是错误的原因。试试这个。

<tbody>
             @foreach($paginationRecord as $i=>$user)
              @php
                 $currentRecordno = 1;
              @endphp  

              <tr>
                <td>{{ $user['ID'] }}</td>
                <td>{{ $user['NICK_NAME'] }}</td>
                <td>{{ $user['PWD_INVALID_HIT'] }}</td>
             </tr>
             @endforeach 

【讨论】:

  • HI ...我已经尝试但它返回错误未定义索引:ID
  • 它告诉你这里的未定义索引是什么?
  • 它说未定义的 ID
  • 我只是通过 addind dd($user) 编辑我的问题...也许你可以帮助我如何显示
  • 你有嵌套数组。循环通过$user 将解决问题
【解决方案2】:

我认为这是因为您的 $user 变量中有一个集合。您需要像这样循环该 $user 变量:

<tbody>
@foreach($paginationRecord as $i=>$user)
   @php
     $currentRecordno = 1;
   @endphp  
   @foreach($user as $i => $u)
   <tr>
       <td>{{ $u['ID'] }}</td>
       <td>{{ $u['NICK_NAME'] }}</td>
       <td>{{ $u['PWD_INVALID_HIT'] }}</td>
   </tr>
   @endforeach
@endforeach 

另外,你需要这个吗:

@php
   $currentRecordno = 1;
@endphp 

foreach 循环中的 $i 应该代表索引或 $currentRecordno。但如果你有充分的理由,那就坚持下去。

【讨论】:

    【解决方案3】:

    为了可读性:

    但实际上,我可能不会做那个额外的 foreach 循环。

    我认为你的控制器方法应该更像:

    public function index()
    {
            $response =  $this->client->get('getUserIndex')->getBody();
             $content = json_decode($response->getContents());
             $paginationRecord = CollectionPaginate::paginate($content['data'], count($content), '15');
    
             return view('configuration.comuserprofiles.ComUserProfilesList', ['paginationRecord' => $paginationRecord]);
    
    }
    

    实际上你的刀片需要改成这样:

    <tbody>
    @foreach($paginationRecord as $i=>$user)
       @php
         $currentRecordno = 1;
       @endphp  
       <tr>
           <td>{{ $user['ID'] }}</td>
           <td>{{ $user['NICK_NAME'] }}</td>
           <td>{{ $user['PWD_INVALID_HIT'] }}</td>
       </tr>
    @endforeach 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-26
      • 1970-01-01
      • 1970-01-01
      • 2015-09-22
      • 2017-03-20
      • 2014-03-25
      相关资源
      最近更新 更多