无法访问 Laravel 模型中的属性

Can't access attributes in Laravel Model

目前我已经为数据库做了一个默认模型user_domains

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

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

}

到目前为止,还不错。

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

我做了一个函数,这个函数使用 all 方法来获取所有数据,没有任何困难的 where-statements。但是,这个输出是一个巨大的集合,包含数据库的每个细节

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) "*"
      }
    }
  }
}

我怎样才能访问 table 中的数据,而不是所有这些垃圾?

$domainsDomainsCollection。不只是一个 Domain.
您正在尝试从此 Collection.

访问 Domain 对象的 attribute

由于您没有在问题中发布您尝试做的事情,我无法提供解决方案。

但我会分享一些知识。

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

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

将所有名称获取到另一个集合

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