3

I have the following data structure:

[
   {
      "id": 1,
      "customer_id": "11",
      "customer_name": "Vic",
      "cheque_withdraw": {
         "id": 2,
         "request_date", "2019-03-30"
      }
   }
]

I have gotten this data from multiple tables through the following code;

$paginator = Cheque::with(['chequeWithdraw' => function($query){
        $query->where('withdraw_status' , 'withdrawn');
    }])
    ->paginate(5);

How do I access the nested json object request_date without having to convert my collection to an associative array? The reason why I do not want to convert the collection to an associative array is because I am having challenges paginating the data that has been json decoded. I am currently trying to access the request_date in my blade file this way;

@foreach($paginator as $cheque)
   {{ $cheque->cheque_withdraw->request_date }}
@endforeach

However, I am getting the following error;

Trying to get property 'request_date' of non-object
3
  • 1
    If you try to print_r($cheque); and die(); inside your foreach What do you get? Commented Mar 18, 2019 at 9:11
  • @pr1nc3 I am getting objects of the two models I used. The Cheque model and ChequeWithdraw model. The result is too long else I would have pasted it here for you to have a look Commented Mar 18, 2019 at 9:18
  • Then can you show me the code that you return the data to the view? Commented Mar 18, 2019 at 9:19

1 Answer 1

5

Actually you have chequeWithdraw and not cheque_withdraw

@foreach($paginator as $cheque)
   {{ $cheque->chequeWithdraw->request_date  ?? 'NA'}}
@endforeach

You might be trying to convert laravel collection to json or array. laravel convert camel case to snake case . So you were getting it for json not for laravel template.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer. It works wonderfully. Kindly explain to me why the object is chequeWithdraw and not cheque_withdraw as it is in the json?
@gerry: you might be trying to convert laravel collection to json or array. laravel convert camel case to snake case . So you were getting it for json not for laravel template.

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.