创建laravel 5 API时无法获取所需的json格式

时间:2018-09-20 14:41:01

标签: json api laravel-5

我有以下require模型,而l - Length changing f - Filtering input t - The Table! i - Information p - Pagination r - pRocessing < and > - div elements <"#id" and > - div with an id <"class" and > - div with a class <"#id.class" and > - div with an id and class 有很多$('table.tableClass').DataTable({ "dom": '<"top"f>rt<"bottom"lp><"clear">' });

Template

我在TestCategory

中定义了以下Test方法
class Template extends Model
{
protected $table = 'test_templates';

protected $fillable = [
    'customers_id',
    'tests_categories_id',
    'tests_id'
];

public function customers(){
    return $this->belongsTo(Customer::class, 'customers_id');
}

public function testCategories(){
    return $this->belongsTo(TestCategory::class, 'tests_categories_id');
}

public function tests(){
    return $this->belongsTo(Test::class, 'tests_id');
}
}

我得到以下json输出:

index

我想按照TemplateController收集所有public function index($customerId) { $templates = Template::with( 'customers','testCategories','tests') ->where('customers_id', $customerId)->get(); $templates = $templates->groupBy('tests_categories_id'); $templates = json_decode($templates, true); $templates = array_values($templates); return response()->json(['Status' => True, 'template' => $templates]); } ,而不必重新打印{ "Status": true, "template": [ [ { "id": 1, "customers_id": 1, "tests_categories_id": 1, "tests_id": 1, "created_at": "2018-09-10 11:03:44", "updated_at": "2018-09-10 11:03:44", "customers": { "id": 1, "name": "Queen PVT.Ltd", "address": "gotham", "contact_number": "9842********", "created_at": "2018-09-10 11:03:43", "updated_at": "2018-09-10 11:03:43" }, "test_categories": { "id": 1, "name": "General Tests", "created_at": null, "updated_at": null }, "tests": { "id": 1, "tests_category_id": 1, "name": "Lamination", "created_at": null, "updated_at": null } }, { "id": 2, "customers_id": 1, "tests_categories_id": 1, "tests_id": 2, "created_at": "2018-09-10 11:03:44", "updated_at": "2018-09-10 11:03:44", "customers": { "id": 1, "name": "Queen PVT.Ltd", "address": "gotham", "contact_number": "984*******", "created_at": "2018-09-10 11:03:43", "updated_at": "2018-09-10 11:03:43" }, "test_categories": { "id": 1, "name": "General Tests", "created_at": null, "updated_at": null }, "tests": { "id": 2, "tests_category_id": 1, "name": "Paper Type", "created_at": null, "updated_at": null } } ], [ { "id": 7, "customers_id": 1, "tests_categories_id": 2, "tests_id": 8, "created_at": "2018-09-10 11:03:44", "updated_at": "2018-09-10 11:03:44", "customers": { "id": 1, "name": "Queen PVT.Ltd", "address": "gotham", "contact_number": "984******", "created_at": "2018-09-10 11:03:43", "updated_at": "2018-09-10 11:03:43" }, "test_categories": { "id": 2, "name": "Scratch Test", "created_at": null, "updated_at": null }, "tests": { "id": 8, "tests_category_id": 2, "name": "HRN Visibility", "created_at": null, "updated_at": null } } ] ] } tests,如下所示:

test_categories

1 个答案:

答案 0 :(得分:0)

如果我正确理解了问题,那么当您期望某些测试与同一模板相关时,会将测试分为不同的模板。

这是因为您的Template类正在存储它所属的test_id,并且每个Template只能存储1个测试。因此,当您致电Template::with('tests')时,它只能返回一个Test::class.

因为您期望看到许多测试属于一个模板,并且当您查看从模板到测试的关系时,您希望模板具有许多测试。因此,您的Template类应实现hasMany关系,如下所示:

class Template extends Model
{

protected $fillable = [
    'customers_id',
    'tests_categories_id' // notice the removal of the test_id which should also be removed from the test_templates migration
];
    ...

    public function tests(){
        return $this->hasMany(Test::class); //change belongsTo to hasMany
    }

    ...
}

应该将test_id存储在Test类中,而不是将template_id存储在Template类中,如下所示:

class Test extends Model
{

protected $fillable = [
    ...
    'test_template_id' // This should be added to $fillable and the tests migration
    ...
];
    ...

    public function template(){
        return $this->belongsTo(Template::class, test_template_id);
    }

    ...
}

现在,当您调用Template::with('tests')时,它可以返回许多Test::class并且应该与您的预期输出匹配。