Laravel Eloquent: How to automatically fetch relations when serializing through toArray/toJson
發表於 : 2019-09-03 16:11:37
https://stackoverflow.com/questions/215 ... ng-through
protected $with = array('user', 'replies');
protected $with = array('user', 'replies');
代碼: 選擇全部
<?php
class Post extends Eloquent
{
protected $table = 'posts';
protected $fillable = array('parent_post_id', 'user_id', 'subject', 'body');
protected $with = array('user', 'replies');
public function user()
{
return $this->belongsTo('User');
}
public function replies()
{
return $this->hasMany('Post', 'parent_post_id', 'id');
}
}