我有这样的代码,是relatioship它自己的表:Laravel 5的Json与关系
类别型号
class Cateogry extends Model {
public function subCategory(){
return $this->hasMany('\App\SubCategory');
}
}
子类别型号
<?php
class SubCategory extends Model {
public function Category(){
return $this->belongsTo('\App\Category');
}
}
在你的控制器
SomeController
<?php
class SomeController extends Controller {
public function index(){
return Category::with('subCategory')->get()
}
}
当我做返回Category :: with('subCategory') - > get(); 他打印:
{
"id": 43,
"name": "Quartos",
"subcategories_id": null,
"sub_category": [
{
"id": 43,
"name": "Quartos",
"subcategories_id": null
}
]
}
{
"id": 55,
"name": "Jovenil",
"subcategories_id": 2,
"sub_category": [
{
"id": 55,
"name": "Jovenil",
"subcategories_id": 2
}
]
}
代替:
{
"id": 43,
"name": "Quartos",
"subcategories_id": null,
"sub_category": [
{
"id": 50,
"name": "Casal",
"subcategories_id": 1
}
{
"id": 51,
"name": "Jovenil",
"subcategories_id": 2
}(...)
]
}
为什么查询会是什么?
我不明白。请修正您的拼写错误,并解释更彻底的是怎么回事 – OskarD90