【发布时间】:2021-01-10 05:20:05
【问题描述】:
我是这个问题的新手,所以请多多包涵。
我使用 Eloquent 作为我的 PHP 数据库库。所以我创建了一个从Illuminate\Database\Eloquent\Model 扩展的类,并尝试查询一条记录。当我打印结果时,我知道它正在获取信息,正如您可以通过受保护的属性看到的那样,但不知何故,记录的公共属性为 NULL。
我错过了一些以前的配置,还是有其他原因?
这是我的结构:
模特,Plantilla.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Plantilla extends Model
{
/**
* @var string
*/
protected $primaryKey = 'cod_plantilla';
/**
* @var string
*/
protected $table = 'plantilla';
protected $connection = 'mysql';
public function __construct()
{
#attributes
parent::__construct();
Database2::init();
}
}
Database.php
<?php
namespace App\Models;
use Illuminate\Database\Capsule\Manager as Capsule;
class Database2
{
private static $db;
static public function init()
{
if (is_null(self::$db)) {
$capsule = new Capsule;
$capsule->addConnection([
'driver' => 'mysql',
'host' => getenv('DB_HOST'),
'database' => getenv('DB_NAME'),
'username' => getenv('DB_USER'),
'password' => getenv('DB_PASS'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
], 'mysql');
// Make this Capsule instance available globally via static methods... (optional)
$capsule->setAsGlobal();
// Setup the Eloquent ORM... (optional; unless you've used setEventDispatcher())
$capsule->bootEloquent();
}
}
}
index.php
$p = Plantilla::where('cod_plantilla', 35)->first();
var_dump($p);
结果
object(App\Models\Plantilla)[251]
protected 'primaryKey' => string 'cod_plantilla' (length=13)
protected 'table' => string 'plantilla' (length=9)
protected 'connection' => string 'mysql' (length=5)
# Values I need
public 'cod_area_interna' => null
public 'cod_tipo_plantilla' => null
public 'nombre' => null
public 'detalle' => null
public 'personalizada' => null
public 'fecha' => null
# Values I need
protected 'keyType' => string 'int' (length=3)
public 'incrementing' => boolean true
protected 'with' =>
array (size=0)
empty
protected 'withCount' =>
array (size=0)
empty
protected 'perPage' => int 15
public 'exists' => boolean true
public 'wasRecentlyCreated' => boolean false
# Same values I need but they're protected
protected 'attributes' =>
array (size=7)
'cod_plantilla' => int 35
'cod_area_interna' => int 2
'cod_tipo_plantilla' => int 1
'nombre' => string 'Some' (length=32)
'detalle' => string 'Some' (length=142)
'personalizada' => null
'fecha' => string '2020-06-25 12:15:13' (length=19)
protected 'original' =>
array (size=7)
'cod_plantilla' => int 35
'cod_area_interna' => int 2
'cod_tipo_plantilla' => int 1
'nombre' => string 'Some' (length=32)
'detalle' => string 'Some' (length=142)
'personalizada' => null
'fecha' => string '2020-06-25 12:15:13' (length=19)
protected 'changes' =>
...
如文档所述,您可以这样做
<?php
$flights = App\Models\Flight::all();
foreach ($flights as $flight) {
echo $flight->name;
}
因此您可以访问属性,即表列值。
就我而言,这些是:
- 鳕鱼植物
- cod_area_interna
- cod_tipo_plantilla
- 名词
- 详细介绍
- 个性化
- fecha
【问题讨论】:
-
Welcome to SO ...没有“公共”属性,属性数组是“受保护的”...虽然有多种方法可以访问模型上的这些属性...你是什么现在真的在找吗?
-
我不明白你的问题实际上在问什么......你有一个模型实例,而不是
null,它是表中记录的表示,字段是属性。 ..你不知道如何访问模型的属性? -
我编辑了提示,这样会更清楚
标签: php mysql laravel eloquent orm