无法访问Laravel模型中的属性

时间:2018-10-04 12:30:26

标签: php laravel model-view-controller model laravel-5.7

当前,我为数据库user_domains设置了默认模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Domains extends Model
{
    public $incrementing = true;
    protected $table = 'user_domains';
    protected $primaryKey = 'id';

}

到目前为止,太好了。

但是,当我使用查询生成器从中获取一些数据时,它不允许我通过attibutes(例如$ domains-> domain

)访问它。

我已经做了一个函数,这个函数正在使用all方法来获取所有数据,而没有任何困难的where语句。但是,它的输出是一个庞大的集合,其中包含数据库的每个细节

object(Illuminate\Database\Eloquent\Collection)#274 (1) {
  ["items":protected]=>
  array(3) {
    [0]=>
    object(App\Domains)#275 (26) {
      ["incrementing"]=>
      bool(true)
      ["table":protected]=>
      string(12) "user_domains"
      ["primaryKey":protected]=>
      string(2) "id"
      ["connection":protected]=>
      string(5) "mysql"
      ["keyType":protected]=>
      string(3) "int"
      ["with":protected]=>
      array(0) {
      }
      ["withCount":protected]=>
      array(0) {
      }
      ["perPage":protected]=>
      int(15)
      ["exists"]=>
      bool(true)
      ["wasRecentlyCreated"]=>
      bool(false)
      ["attributes":protected]=>
      array(6) {
        ["id"]=>
        int(1)
        ["domain"]=>
        string(10) "example.nl"
        ["user_id"]=>
        int(1)
        ["verified"]=>
        int(1)
        ["created_at"]=>
        NULL
        ["updated_at"]=>
        NULL
      }
      ["original":protected]=>
      array(6) {
        ["id"]=>
        int(1)
        ["domain"]=>
        string(10) "example.nl"
        ["user_id"]=>
        int(1)
        ["verified"]=>
        int(1)
        ["created_at"]=>
        NULL
        ["updated_at"]=>
        NULL
      }
      ["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\Domains)#276 (26) {
      ["incrementing"]=>
      bool(true)
      ["table":protected]=>
      string(12) "user_domains"
      ["primaryKey":protected]=>
      string(2) "id"
      ["connection":protected]=>
      string(5) "mysql"
      ["keyType":protected]=>
      string(3) "int"
      ["with":protected]=>
      array(0) {
      }
      ["withCount":protected]=>
      array(0) {
      }
      ["perPage":protected]=>
      int(15)
      ["exists"]=>
      bool(true)
      ["wasRecentlyCreated"]=>
      bool(false)
      ["attributes":protected]=>
      array(6) {
        ["id"]=>
        int(2)
        ["domain"]=>
        string(11) "example.com"
        ["user_id"]=>
        int(1)
        ["verified"]=>
        int(1)
        ["created_at"]=>
        NULL
        ["updated_at"]=>
        NULL
      }
      ["original":protected]=>
      array(6) {
        ["id"]=>
        int(2)
        ["domain"]=>
        string(11) "example.com"
        ["user_id"]=>
        int(1)
        ["verified"]=>
        int(1)
        ["created_at"]=>
        NULL
        ["updated_at"]=>
        NULL
      }
      ["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) "*"
      }
    }
    [2]=>
    object(App\Domains)#277 (26) {
      ["incrementing"]=>
      bool(true)
      ["table":protected]=>
      string(12) "user_domains"
      ["primaryKey":protected]=>
      string(2) "id"
      ["connection":protected]=>
      string(5) "mysql"
      ["keyType":protected]=>
      string(3) "int"
      ["with":protected]=>
      array(0) {
      }
      ["withCount":protected]=>
      array(0) {
      }
      ["perPage":protected]=>
      int(15)
      ["exists"]=>
      bool(true)
      ["wasRecentlyCreated"]=>
      bool(false)
      ["attributes":protected]=>
      array(6) {
        ["id"]=>
        int(3)
        ["domain"]=>
        string(11) "example.org"
        ["user_id"]=>
        int(1)
        ["verified"]=>
        int(1)
        ["created_at"]=>
        NULL
        ["updated_at"]=>
        NULL
      }
      ["original":protected]=>
      array(6) {
        ["id"]=>
        int(3)
        ["domain"]=>
        string(11) "example.org"
        ["user_id"]=>
        int(1)
        ["verified"]=>
        int(1)
        ["created_at"]=>
        NULL
        ["updated_at"]=>
        NULL
      }
      ["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 个答案:

答案 0 :(得分:1)

$domainsCollection中的Domains。不只是一个Domain
您正在尝试从此attribute访问Domain对象的Collection

由于您尚未发布要在问题中尝试的操作,因此我无法提供解决方案。

但是我会分享一些知识。

从集合中获取一个元素,然后可以访问该对象的属性。

// name of the first domain (string)
$name = $domains->get(0)->name;

将所有名称保存到另一个收藏集中

// names of all the domains (another collection)
$names = $domains->map->name;