【问题标题】:unable to get desired json format while creating laravel 5 api创建 laravel 5 api 时无法获得所需的 json 格式
【发布时间】:2019-02-24 22:03:05
【问题描述】:

我有以下Template 模型和TestCategory hasMany 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');
}
}

我在TemplateController 中定义了以下index 方法

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

我得到以下 json 输出:

{
"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
            }
        }
    ]
]
}

我想按照test_categories收集所有的tests,而不需要转载customerstest_categories如下:

{
"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": "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": 1,
                "tests_category_id": 1,
                "name": "Lamination",
                "created_at": null,
                "updated_at": null
                },
                {
                "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": "984265*****",
                "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收集所有tests
  • 你真的希望有人能看到每一分钟的细节,解释有什么不同
  • 我想得到{Template : { customer : {}, test_category : { tests:{tests1, test2}}},但我得到了{Template1 : { customer : {}, test_category : { tests:{tests1}},Template2 : {customer : {}, test_category : { tests:{tests2}}}

标签: json api laravel-5


【解决方案1】:

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

这是因为您的 Template 类正在存储它所属的 test_id,并且每个模板只能存储 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
    }

    ...
}

您应该将template_id 存储在您的测试类中,而不是在模板类中存储test_id,看起来像这样:

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,并且应该与您的预期输出匹配。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-14
    • 2021-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多