php - 我需要帮助定义 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]。”
请帮我解决这个问题,以便我收集。
解决方案
请试试这个
$events = Event::with('memoryof')->get()->sortByDesc('created_at');
dd($events)
推荐阅读
- mysql - Slurmdbd 和 ssl
- python - auto_arima() 将 (p,d,q) 的最佳值返回为 (0,0,0)
- php - 为什么在进行有效用户检查后页面无法正常工作?
- javascript - 保存在 Wordpress 用户配置文件中的新字段
- django - Django 验证特定问题的答案
- angular - 使用 Angular、RxJS 以 Facade 模式解析异常的 http 响应
- java - 通过 Java ASM 调用私有方法
- iot - FIWARE 的 IoT 代理中的惰性属性
- lua - 如何获取在 Lua 中加载的脚本的确切路径?
- clickhouse - 代码:210。 DB::NetException:连接被拒绝(本地主机:9000)