尝试获取非对象 Laravel 5.4 的 属性

Trying to get property of non - object Laravel 5.4

我还是新手,正在尝试从 REAXML 文件保存数据。

https://github.com/i4ucode/reaxml

解析 REXML 的供应商

路线:

Route::get('/admin/synctest', 'Admin\AdminController@synctest');

型号属性

class Property extends Model 
{
   protected $table = 'properties';
   protected $primaryKey = 'id';
   protected $fillable = ['refID', 'propkind_id', 'other column'];
   public function Kind()
   {
      return Propkind::findOrfail($this->propkind_id)->name;
   }
   //other public function
}

模型Propkind

class Propkind extends Model
{
    protected $table = 'propkinds';
    protected $primaryKey = 'id';
    protected $fillable = ['id', 'name', 'slug'];
    public function Property(){
        return $this->hasMany('App\Property');
    }
    //other public function
}

控制器

public function synctest()
{
    // get new class
    $processor = new \REA\XmlProcessor();
    $directories = Storage::disk('reaxml')->files();

    foreach ($directories as $zipFile) {
        //get XML file
        $files = file_get_contents(url('feed/' . $zipFile));
        //process the xml
        $properties = $processor->parseXmlString($files);
        //loop
        foreach ($properties as $property) {

            Property::updateOrCreate(

                ['refID' => $property->getRefID()],
                [
                    //other code
                    'propkind_id' => Propkind::where('name', $property->getCategoryNaskleng())->first()->id, ===> ErrorException (E_NOTICE) Trying to get property of non-object
                ]

            );
        }
    }
}

这段代码Propkind::where('name', $property->getCategoryNaskleng())->first()->id) ==>显示数据但显示

ErrorException Trying to get property of non-object.

某种启迪,智慧,不胜感激。

dd()dump() 之间的区别是 dd() 停止脚本的执行(死亡),dump() 显示参数的表示但它不'停止脚本。 出于这个原因,我猜错误(通知)是在执行您发布的这段代码之后出现的。

clearly one of the values of $properties doesn't return a model

几个小时后,我发现 null 是问题所在。 感谢@Devon 的洞察力。 @simonecosci 和@Leena Patel 发表评论。