1
votes

My foreach return a different array when there is different $Tag i need My Foreach to return all tasks in 1 array not multi because I can't sort them correctly

@foreach ($Paths->pathtags as $Tag)
   {{ $Tag->name }}
     @foreach ($Tag->Tasks as $Task)

       {{ $Task->id   }}
       {{ $Task->name }}
     @endforeach
@endforeach

in Example

Tag Name : Tag1 , Tag2
Task : id 11
Task Name : Task number 1
................................................

Tag Name : Tag4 , Tag6
Task : id 7
Task Name : Task number 2

................. Tag Name : Tag4 , Tag6
Task : id 8
Task Name : Task number 3

So if the Tags is different I can't sort them only tasks with same tags is able to be sort

1
This is very confusing... Could you give some more info, how you retrieve the data and what you're ctually trying to accomplish? foreach doesn't return the multiple arrays... that's the way you built your data structure or retrieve the data from the DB - Edwin Krause
^ I can't quite understand what you're trying to accomplish, but it sounds like this might be a candidate for one or more helpers built-in with Laravel collections. For example, the flatten function. - sheng
Give an example of the actual behavior and the desired behavior. - Laerte

1 Answers

0
votes

I think you are trying to flatten the relationship Tasks. You can override the toArray function in your model, so you will format the returned value in your controller:

In your Controller:

$results = YourModel::with('Tasks')->get() $Paths = $results->toArray();

In your Model

public function toArray() { return [ ... 'taskId' => $this->Tasks->id, 'taskName' => $this->Tasks->name, ]; }