Laravel return当我尝试返回模型时,偏移量类型非法


Laravel return Illegal offset type when I trie return a model

我使用的是一个从数据库中返回多个数据的大型json,我需要返回一个没有任何关系的整数模型,我只需要返回LampModels用这个很棒的json建模的所有记录。但是Laravel总是返回非法偏移类型。

控制器

public function showAllUdiJson()
  {
    $allLamps = LampModels::all();
    return Ilumination::with('street')
                        ->with('neighborhood')
                        ->with('iluminationinfo')
                        ->with('economyplan')
                        ->with('lamp')
                        ->with('reactor')
                        ->with('aluminumcable')
                        ->with('steelconduit')
                        ->with('alllamps', $allLamps)
                        ->with('ticket')->get();
  }

LampModels

<?php
class LampModels extends 'Eloquent {
    protected $fillable = [];
  protected $table = 'lampmodel';
}

照明

<?php
class Ilumination extends 'Eloquent {
    protected $fillable = [];
  protected $table = 'ilumination';
  public function street()
  {
    return $this->belongsTo('street');
  }
  public function neighborhood()
  {
    return $this->hasOne('neighborhood', 'id');
  }
  public function iluminationinfo()
  {
    return $this->hasOne('iluminationinfo');
  }
  public function ticket()
  {
    return $this->hasMany('ticket');
  }
  public function economyplan()
  {
    return $this->hasOne('economyplan', 'id' ,'street_id');
  }
  public function lamp()
  {
    return $this->hasOne('lamp', 'id');
  }
  public function reactor()
  {
    return $this->hasOne('reactor', 'id');
  }
  public function aluminumcable()
  {
    return $this->hasOne('aluminumcable', 'id');
  }
  public function steelconduit()
  {
    return $this->hasOne('steelconduit', 'id');
  }
}

请参阅错误

您的错误报告非常糟糕,但您的Ilumination模型似乎没有alllamps方法。

你应该用关系把LampModels附加到你的发光模型上,而不是做你正在做的事情,因为这是一种错误的方法。

我认为您访问了在Illumination Model中创建的某处票证方法,该方法遇到了偏移错误。。

public function ticket()
{
  return $this->hasMany('ticket');
}

如果你想访问照明->票证,你必须使用这个方法和循环。

foreach(illumination->tickets as ticket) {
     $field1 =  ticket->field1;
}

如果您仍然面临任何问题,请在此处分享您的错误日志页面。。