php - Laravel 在 Web 服务器的 View 中显示数据

标签 php mysql laravel laravel-5 entity-relationship

它给我一个“尝试获取非对象的属性”的错误,你能帮我吗?它在我的离线服务器上正常工作,但是如果我将它上传到我的实时服务器上,它会出错

如果我 dd 我的 Controller 给出结果。

Collection {#287 ▼
  #items: array:3 [▼
    0 => Branch {#297 ▼
      #connection: "mysql"
      #table: null
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:6 [▶]
      #original: array:6 [▶]
      #casts: []
      #dates: []
      #dateFormat: null
      #appends: []
      #events: []
      #observables: []
      #relations: array:1 [▼
        "cashier" => User {#301 ▼
          #fillable: array:4 [▶]
          #hidden: array:2 [▶]
          #connection: "mysql"
          #table: null
          #primaryKey: "id"
          #keyType: "int"
          +incrementing: true
          #with: []
          #withCount: []
          #perPage: 15
          +exists: true
          +wasRecentlyCreated: false
          #attributes: array:8 [▼
            "id" => 5
            "name" => "qqqaMiracle Auer"
            "email" => "qqqapdickens@hotmail.com"
            "password" => "$2y$10$FArFw2M.dQBcsP4XAQ8a5.vYsfZHLp8S/6.1ZSaVGHaxuSKiYogbe"
            "usertype" => "cashier"
            "remember_token" => null
            "created_at" => "2018-01-22 03:30:20"
            "updated_at" => "2018-01-22 05:56:19"
          ]
          #original: array:8 [▶]
          #casts: []
          #dates: []
          #dateFormat: null
          #appends: []
          #events: []
          #observables: []
          #relations: []
          #touches: []
          +timestamps: true
          #visible: []
          #guarded: array:1 [▶]
          #rememberTokenName: "remember_token"
        }
      ]
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #fillable: []
      #guarded: array:1 [▶]
    }
    1 => Branch {#298 ▶}
    2 => Branch {#299 ▶}
  ]
}

但是当我让我的代码显示收银员的名字时。使用此代码

@foreach($dataBranch as $Branch)
<tr class="item{{$Branch->id}}">
<td> <a class="name">{{$Branch->branch_name}}</a> </td>
<td> <a class="name">{{$Branch->cashier->name}}</a> </td>
</tr>
@endforeach

它给我一个“尝试获取非对象的属性”的错误,你能帮我吗?它在我的离线服务器上正常工作,但是如果我将它上传到我的实时服务器上,它会出错

请帮忙

最佳答案

首先你需要调试foreach中的数据,然后尝试在条件下使用is_object() php函数来获取正确的数据字段。

您可以通过这种方式验证您的数据:

<?php 
//verify your data
foreach($dataBranch as $Branch) {

var_dump($Branch);

if(is_object($Branch)) {
  if(!empty($Branch->id)) {
   echo '$Branch->id';
  }
}}
exit;
?>

关于php - Laravel 在 Web 服务器的 View 中显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49344315/

相关文章:

php - 在 PHP 中创建一个新的 "command"

Mysql,连接一个表并具有来自不同行的多个条件

MYSQL - 无法连接到 'localhost' 上的 MYSQL 服务器 (10061)

php - 如何在 PHP 中使用存储过程在日期列中传递 null?

php - 获取数据时在数组中显示空白输入数组值 Laravel

laravel - 以编程方式渲染 Laravel 7 组件

php - 指定没有订单的客户

PHP:从递归函数返回一个数组

php - 简单的登录脚本返回错误

php - Laravel - 用不同的值更新多个记录