0

in this below result of fetch data:

{
    "purchases": [
        {
            "id": 1,
            "member_id": 1,
            "mediator_id": 2,
            "type": null,
            "cash": 38712402,
            "created_at": "2017-11-08 06:40:45",
            "updated_at": "2017-11-08 06:40:45",
            "deleted_at": null,
            "cheques": [
                {
                    "id": 1,
                    "purchase_id": 1,
                    "expire_date": "1396-01-19",
                    "amount": 97094060,
                    "isRejected": 0,
                    "created_at": "2017-11-08 06:40:45",
                    "updated_at": "2017-11-08 06:40:45",
                    "deleted_at": null
                }
            ]
        },
        {
            "id": 2,
            "member_id": 1,
            "mediator_id": 2,
            "type": null,
            "cash": 46760191,
            "created_at": "2017-11-08 06:40:45",
            "updated_at": "2017-11-08 06:40:45",
            "deleted_at": null,
            "cheques": []
        }
    ]
}

which i get that by this code:

$purchases = \App\Purchase::with('cheques')->where('member_id',
        $request->userId)->get();

i'm trying to get amount from cheques and 'cash','created_at' from purchases where 'member_id'=$request->userId, but i get empty cheques when i use this code:

$purchases = \App\Purchase::with(array('cheques'=>function($query){
        $query->select('amount');
    }))->where('member_id',$request->userId)->get(['cash','created_at']);

3 Answers 3

1

Can you try this?

$purchases = \App\Purchase::with(array('cheques'=>function($query){
    $query->select('id', 'amount', 'purchase_id');
}))->where('member_id',$request->userId)->get(['cash','created_at']);
Sign up to request clarification or add additional context in comments.

Comments

0

Try the Eager Loading Specific Columns part from the documentation- https://laravel.com/docs/5.5/eloquent-relationships

It says:

When using this feature, you should always include the id column in the list of columns you wish to retrieve.

Comments

0
$purchases = \App\Purchase::with(
    array('cheques' => function($query){
        $query->select('amount');
    })
    )->where('member_id',$request->userId)
    ->select('cash','created_at')->get();

If you want to select columns using get methon of collection, you need to pass id:

->get(['id', 'cash', 'created_at']); or you can use ->select('cash', 'created_at')->get();

Which you prefer.

2 Comments

cheques is null again
Can you try $purchases = \App\Purchase::where('member_id',$request->userId)->with(array('cheques' => function($query){$query->select('amount');})->select('cash','created_at')->get();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.