雄辩充满了关系

时间:2018-04-12 04:48:13

标签: eloquent

class Market extends Model{
    protected $table = 'Markets';

    protected $primaryKey = 'maid';

    protected $guarded = ['maid'];

    protected $casts = ['trade' => 'boolean'];

    protected $with = ['textbook'];

    public $timestamps = false;

    public function textbook(){
        return $this->hasMany(Textbook::class, 'isbn13', 'isbn13');
    }
}





class MarketController extends Controller{
    public function get($request, $response){
        $market = Market::with('textbook')->paginate();

        return $response->withJson($market);
    }   

}

我有一个Eloquent Collection和一些关系让我们说市场和教科书。我的关系工作得很好。我的问题是我如何修改我的集合嵌套。也就是说,现在它返回有关该书的信息,然后我可以加载关系教科书。 目前我有:

{
        "maid": 1,
        "meid": 388,
        "coid": 139719,
        "isbn13": "9780718197049",
        "quality": 8,
        "price": 99,
        "trade": 0,
        "notes": "Brand new book at used price!",
        "timecard": "2018-01-08 07:38:38",
        "textbook": []
}

但我想要这样的事情:

{
    "maid": 1,
    "meid": 388,
    "coid": 139719,
    "isbn13": "9780718197049",
    "quality": 8,
    "price": 99,
    "trade": 0,
    "notes": "Brand new book at used price!",
    "timecard": "2018-01-08 07:38:38",
    "textbook": [
        {
            "isbn13": 2147483647,
            "isbn10": "0718197046",
            "title": "Economics: The User's Guide",
            "subtitle": "A Pelican Introduction",
            "authors": "Ha-joon Chang",
            "publisher": "Penguin UK",
            "published": "2014-05-01",
            "edition": null,
            "volume": null,
            "pages": 304,
            "categories": "Business & Economics",
            "description": "What is economics? What can - and can't - it explain about the world? Why does it matter? Ha-Joon Chang teaches economics at Cambridge University, and writes a column for the Guardian. The Observer called his book 23 Things They Don't Tell You About Capitalism, which was a no.1 bestseller, 'a witty and timely debunking of some of the biggest myths surrounding the global economy.' He won the Wassily Leontief Prize for advancing the frontiers of economic thought, and is a vocal critic of the failures of our current economic system.",
            "thumbnail": "http://books.google.com/books/content?id=93rAAgAAQBAJ&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api"
        }
    ]
}

如何完成上述(嵌套)结果以及放置该代码的位置!

0 个答案:

没有答案
相关问题