0

目前我已经为数据库创建了一个默认模型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 语句。但是,这个输出是一个包含数据库每个细节的巨大集合

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

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

4

1 回答 1

2

$domains是一个CollectionDomains不只是一个Domain
您正试图从 this 访问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;
于 2018-10-04T12:38:12.640 回答