Laravel Eloquent: How to automatically fetch relations when serializing through toArray/toJson

http://laravel.com/

http://kejyun.github.io/Laravel-4-Docum ... roduction/
回覆文章
yehlu
Site Admin
文章: 3244
註冊時間: 2004-04-15 17:20:21
來自: CodeCharge Support Engineer

Laravel Eloquent: How to automatically fetch relations when serializing through toArray/toJson

文章 yehlu »

https://stackoverflow.com/questions/215 ... ng-through


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');
    }

}

回覆文章

回到「laravel」