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

尝试获取非对象Laravel 5.4的属性,laravel,laravel-5.4,Laravel,Laravel 5.4,我还是新手,正在尝试从REAXML文件保存数据 用于从中解析REAXML的供应商 路线: Route::get('/admin/synctest', 'Admin\\AdminController@synctest'); 模型属性 class Property extends Model { protected $table = 'properties'; protected $primaryKey = 'id'; protected $fillable = ['refID

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

用于从中解析REAXML的供应商

路线:

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
}
实物模型

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)
==>显示数据,但trow

尝试获取非对象的属性时出错异常


某种启示,智慧,非常感谢。

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

显然,$properties的其中一个值不返回模型

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

您不应该依赖first()返回模型。如果找不到模型,它可能返回null,这可能是您的问题所在。您无法在null上获取
id
属性。@Devon仍在思考应该做什么。在依赖属性之前,请始终检查first是否返回null。.尝试
dd(Propkind::where('name',$property->getCategoryAskleng())->first()
它返回`#表:“propkinds”#属性:数组:3[“id”=>1“name”=>“Villa”“slug”=>“villa”]`你有一个循环。。。。显然,$properties的其中一个值不返回模型。。。就像我反复说的。。检查是否为空谢谢您的评论。错误在我发布的代码中
Propkind::where('name',$property->getCategoryNaskleng())->first()->id