首页 > 解决方案 > 我需要帮助定义 Laravel Eloquent 关系

问题描述

两种型号:

class Event extends Model
{  // serves the same role as Post 
    public function memoryof()
    {  
        return $this->hasMany(MemoryOf::class);
    }
}



class MemoryOf extends Model
{  // serves the same role as comments
    public function event()
    {
          return $this->belongsTo(Event::class);
    }
    
}

我检索这样的数据:

$events=Event::with('memories')->get()->sortByDesc('created_at');

这应该给我一个集合。相反,它给出:

“此集合实例上不存在属性 [memoryof]。”

请帮我解决这个问题,以便我收集。

标签: phplaraveleloquent

解决方案


请试试这个

$events = Event::with('memoryof')->get()->sortByDesc('created_at');
dd($events)

推荐阅读