-1

I have the following relations Transaction Table is the parent table for ETransaction and ATransaction and belongs to transactions table

$transactionA= Transaction::with('tAccount')->has('tAccount')->get();
$transactionE= Transaction::with('tExchange')->has('tExchange')->get();
$collection = collect([$transactionE,$transactionA]);

$sorted = $collection->sortBy('created_at') do not work for me

Hewad
  • 23
  • 1
  • 9
  • What kind of data are we talking about? What result did you get with what you tried, and what makes that result not suitable? – Stratadox Dec 13 '19 at 17:18
  • i tried $collection->sort() and $collection->sortBy(key) – Hewad Dec 13 '19 at 17:18
  • I am trying to load data of two tables from database and collect it to one collection i want to sort data inside a collection by created_at key and dont give the suitable result – Hewad Dec 13 '19 at 17:20
  • Then you need to combine the collections you have (`->get()` returns a `Collection`). You can't just call `collect()` on two collections and expect them to combine... Right now, you have a `Collection` of two collections, so calling `sort()` or `sortBy()` will have trouble working as you're expecting. Rethink your logic. – Tim Lewis Dec 13 '19 at 17:22
  • Which result do you get, and what makes it unsuitable? – Stratadox Dec 13 '19 at 17:23
  • It looks like your $collection property is a collection of two collections. That's 3 collections total, not 1 merged collection, which is presumably what @TimLewis points out as well – Stratadox Dec 13 '19 at 17:24
  • it shows data of transactionE table on the top and transactionA table at the end like it show all records of transactionE table first and Then i start to show Data of second Table i want to show it on created_at key in both tables – Hewad Dec 13 '19 at 17:25
  • @Stratadox then how to do deal with this problem any better suggestion please – Hewad Dec 13 '19 at 17:27
  • I hope my answer results in what you're looking for, because your end-goal is still a bit unclear at this point. – Stratadox Dec 13 '19 at 17:35

2 Answers2

1

The main problem you encounter isn't necessarily about sorting, but about building up the initial collection.

Right now, you make two collections, transactionA and transactionE. You then initialize a third collection collection that contains both transactionA and transactionE.

You can think of this as transactionA being a box of records, and transactionE being another box of records. What you want is one big box of records, but the current code puts both boxes in another, bigger box. When sorting the bigger box, all you sort is the order in which the smaller boxes end up in the bigger box.

What you presumably want instead, is to merge the contents of both boxes and sort the ensemble. You can do so by merging the two collections:

$collection = $transactionA->merge($transactionE);
Stratadox
  • 1,291
  • 8
  • 21
1

I'm not sure why you even need separate queries. Stratadox's answer shows how to query, merge and sort, but you can do that in a single query using Eloquent:

$collection = Transaction::with(["tAccount", "tExchange"])
->has("tAccount")
->orHas("tExchange")
->orderBy("created_at")
->get();

In a single query, this will look for all Transaction records that have either a tAccount or tExchange record associated, sort it by the created_at timestamp and return it in a single call. Pushing the logic to the Collection class can be inefficient, so let the database handle it when possible.

Tim Lewis
  • 27,813
  • 13
  • 73
  • 102
  • 1
    Upvoted because this is presumably the most performant solution. @Hewad: See my own answer for what went wrong initially, then apply this solution to get it working. – Stratadox Dec 13 '19 at 17:45
  • 1
    @Stratadox Upvoted yours too! Solution to the root cause of the problem should be the one that's accepted, I like to provide additional options that avoid the root problem. Cheers! – Tim Lewis Dec 13 '19 at 17:47
  • but i have some fields in tExchange which are not in tAccount it throws exception trying to get property of non object wiht orHas mehod – Hewad Dec 13 '19 at 17:48
  • @Hewad Nowhere in your original question are you trying to access `$transaction->tAccount` or `$transaction->tExchange`. If you have a new problem, ask a new question. – Tim Lewis Dec 13 '19 at 17:49
  • no i have the field rate in tExchange that i don't have in tAccount but it throws exception – Hewad Dec 13 '19 at 17:52
  • 1
    **The original question you asked was not how to access that**. If you have a new problem, ask a new question. That is how Stackoverflow works. – Tim Lewis Dec 13 '19 at 17:52