[英]Why query statement results on eloquent orm is returning null values
我是这个问题的新手,所以请耐心等待。
我使用 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();
}
}
}
索引.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;
}
所以你可以访问属性又名表列值。
就我而言,这些是:
我不确定你在这里真正问的是什么。
属性不是类的“属性”。 它们保存在名为$attributes
的受保护数组中。 如果您想访问它们,您可以按照文档说明的方式进行。
您可以通过动态属性访问它们:
$p = Plantilla::find(35);
echo $p->nombre;
通过数组访问:
echo $p['nombre'];
您可以自己获取属性数组:
dump($p->getAttributes());
将模型的属性(和加载的关系)序列化为数组:
dump($p->toArray());
或者甚至将序列化模型作为 JSON:
echo $p->toJson();
在我看来,Eloquent 实际上表现得完全正确!
Eloquent 使用 PHP 魔术方法做了很多自动魔术。 看似普通的 PHP 对象属性实际上是通过$attribute
属性中的__get()
和__set()
动态访问的。
在你的例子中,你有这个:
$p = Plantilla::where('cod_plantilla', 35)->first();
使用 Tinker(如果您是 Laravel 的新手,最容易使用php artisan tinker
来解决这类问题),您应该可以尝试像这样访问您的数据库列:
> $p->cod_plantilla;
35
> $p->fecha;
'2020-06-25 12:15:13'
你应该发现你的值被返回了,即使var_dump()
什么也没显示!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.