简体   繁体   中英

Better way to retrieve data from multiple table using Laravel's Eloquent

On my Quiz App I want to make my code more scalable and I am confused how to optimize it and this one is most confusing.. I have Four tables like

1.Question Table
2.QuestionOption Table
3.QuestionImage Table
4.QuestionSolutionImage Table

The Question Table is linked with all below tables with foreignKey QuestionId

now for getting questionData i execute this code

       $builder = Question::where('TestId', $_testId);
        
       $data = $builder->with(['questionOptions' => function ($query) {
            $query->where('Status',1)
            ->select(['QuestionId', 'id', 'optionText', 'rank', 'isAnswer', 'timeStamp']);
        }]) ->with(['questionImages' => function ($query) {
            $query->where('Status',1)
            ->select(['QuestionId', 'id', 'imagePath', 'thumbPath', 'rank', 'timeStamp']);
        }])->with(['solutionImages' => function ($query) {
            $query->where('Status',1)
            ->select(['QuestionId', 'id', 'imagePath', 'thumbPath', 'rank', 'timeStamp']);
        }]);

        $data = $builder->select([
            'Id', 'uid', 'type', 'rank', 'questionText', 'totalMarks', 'negativeMark as negativeMarks', 'note', 'solutionText', 'status', 'timeStamp',
        ])->get()->toArray();

and i get this output

 [
        {
            "Id": 1,
            "uid": "123456789098760",
            "type": "multiple",
            "rank": 1,
            "questionText": "2 + 2 = ?",
            "totalMarks": 5,
            "negativeMarks": 2,
            "note": null,
            "solutionText": null,
            "status": "active",
            "timeStamp": "2020-11-17 11:42:31",
            "questionOptions": [
                {
                    "QuestionId": 1,
                    "id": 1,
                    "optionText": "5",
                    "rank": 1,
                    "isAnswer": "no",
                    "timeStamp": "2020-11-17 11:42:31"
                },
                {
                    "QuestionId": 1,
                    "id": 2,
                    "optionText": "4",
                    "rank": 2,
                    "isAnswer": "yes",
                    "timeStamp": "2020-11-17 11:42:31"
                },
                {
                    "QuestionId": 1,
                    "id": 3,
                    "optionText": "four",
                    "rank": 3,
                    "isAnswer": "yes",
                    "timeStamp": "2020-11-17 11:42:31"
                },
                {
                    "QuestionId": 1,
                    "id": 4,
                    "optionText": "7",
                    "rank": 4,
                    "isAnswer": "no",
                    "timeStamp": "2020-11-17 11:42:31"
                }
            ],
            "questionImages": [
                {
                    "QuestionId": 1,
                    "id": 1,
                    "imagePath": "12345678",
                    "thumbPath": "thumbPath",
                    "rank": 10,
                    "timeStamp": "2020-11-17 11:42:31"
                },               
            ],
            "solutionImages": [
                {
                    "QuestionId": 1,
                    "id": 1,
                    "imagePath": "storage/app/solution/image/a7bBSNRkgy.jpg",
                    "thumbPath": "storage/app/solution/thumb/a7bBSNRkgy.jpg",
                    "rank": 10,
                    "timeStamp": "2020-12-04 10:32:49"
                }
            ]
        }
    ]

it seems perfect and works fine but from now on as users are increasing this question api is hitting DB too much which results in too many connections issue on DB I have also increased that on DB. But my actual point is can I optimize this code and do it in better way as this code results in multiple hitting on DB. Any help would be appreciated please also if something misunderstood please ask. Thank you !!

You can try using chunk with query builder. If you are using eloquent for large data where you need to manipulate the result then raw sql is quick than eloquent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM