Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel elequent belongsTo在实时服务器上返回键而不是对象,但在开发机器上返回模型_Laravel_Eloquent_Relationships - Fatal编程技术网

Laravel elequent belongsTo在实时服务器上返回键而不是对象,但在开发机器上返回模型

Laravel elequent belongsTo在实时服务器上返回键而不是对象,但在开发机器上返回模型,laravel,eloquent,relationships,Laravel,Eloquent,Relationships,我在拉威尔有一些模特,但其中一个表现很奇怪 这有点尴尬 我设法在我的生产服务器上使用不同于我的开发机器的列名称。。。在我的开发机器上,该列称为creation_id,但在我的生产机器上,它称为creation 我有一个创造物,有许多事件 该事件属于创建,并且有许多EventAttenders EventAttenders属于events,并且可以访问创建->标题,为方便起见,该标题为grand parent 在我的开发机器上,一切都像我期望的那样工作,但是在我的实时服务器上,我从事件->创建

我在拉威尔有一些模特,但其中一个表现很奇怪


这有点尴尬

我设法在我的生产服务器上使用不同于我的开发机器的列名称。。。在我的开发机器上,该列称为creation_id,但在我的生产机器上,它称为creation


我有一个创造物,有许多事件 该事件属于创建,并且有许多EventAttenders EventAttenders属于events,并且可以访问创建->标题,为方便起见,该标题为grand parent

在我的开发机器上,一切都像我期望的那样工作,但是在我的实时服务器上,我从事件->创建中获取id而不是创建

请注意,EventAttende->getEventCreationTitleAttribute在“getCreationTitleAttribute”之前有效,这意味着EventAttende->event()有效

我使用的是Laravel4.1

这是相关代码,从frozenNodes管理员调用getCreationTitleAttribute

// Filename: /app/models/Event.php
<?php
namespace XXX;
class Event extends \Eloquent 
{
    protected $connection = 'legacy';

    public function creation()
    {
        return $this->belongsTo('XXX\Creation', 'creation_id');
    }

    public function getCreationTitleAttribute()
    {
        $x = $this->creation; // I have also tried $this->creation()->first() which returns null on live server

        // This should be the normal behaviour, the test shouldn't even be necessary
        // And the test wasn't there before i uploaded to my live server
        if (is_object($x))
            return $x->title;

        // This is the flow I would expect, 
        // and the flow I get on my production machine...
        return $x;
    }

    public function eventAttende()
    {
        return $this->hasMany('XXX\EventAttende');
    }
}

// Filename: /app/models/Creation.php
<?php
namespace XXX;
class Creation extends \Eloquent 
{
    protected $connection = 'legacy';

    public function Creation()
    {
        $this->bgColor="FFFFFF";
        $this->textColor="000000";
        $this->linkColor="0000FF";
    }

    public function events()
    {
        return $this->hasMany('XXX\Event');
    }
}

// Filename: /app/models/EventAttende.php
<?php
namespace XXX;
class EventAttende extends \Eloquent 
{
    protected $connection = 'legacy';

    // this works
    public function event()
    {
        return $this->belongsTo('XXX\Event', 'event');
    }

    public function getEventCreationTitleAttribute()
    {
        return $this->event()->first()->creationTitle;
    }
}

我设法在我的生产服务器上使用不同于我的开发机器的列名称

  • 在我的开发机器上,列名为creation_id
  • 在我的生产机器上,它被称为创造

首先,您需要该检查,因为relation
creation
返回
Model
null
。可能这就是问题所在,没有返回
id
。如果关系列和表列的名称不相同(不会返回
id
,而是返回该列的值),则可能会出现后者。您好,Jarek,事件表中没有名为creation的列,外键名为creation\u id。显示执行代码如前所述,问题不在于关系返回null,这是可以理解的。。。不太可能,因为无法删除创建,并且事件属于创建。。。问题是它返回的是我的live server上的id,而不是生产机器上的对象我正在使用frozenNodes administrator显示数据。。。因此,我只是设置了“视图”列的定义。在几秒钟内完成我的编辑。
/**
 * The display columns
 */
'columns' => array(
    'id',
    'creation_title' => array(
        'title' => 'Kreation',
        //'name_field' => 'full_name',
        //'type' => 'relationship',
        //'relationship' => 'creation',
    ),
    'name' => array(
        'title' => 'Navn'
    ),

),