8

I need to get only the roomnumber arrays returned from the following query:

$roomnumbers = Room::with(['floorroomcount' => function($query){
                $query->with('roomnumber')->get();
        }])->where('roomtype_id', $roomtype_id)->get();

Tried: The follow pluck is returning floorroomcount

$roomnumbers->pluck('floorroomcount');

but i need roomnumber array, how can i get?

4
  • Did you try $roomnumbers->pluck('floorroomcount.roomnumber');? Commented May 9, 2018 at 11:54
  • yes, it give [null,null,null] Commented May 9, 2018 at 12:18
  • But if you dd($roomnumbers);, the roomnumber data does exist? Commented May 9, 2018 at 12:19
  • yes it exist, room and inside it has floorroomcount and inside it has roomnumber Commented May 9, 2018 at 12:29

5 Answers 5

30

This gives you all roomnumber results in one collection:

$roomnumbers->pluck('floorroomcount')->collapse()->pluck('roomnumber')->collapse();
Sign up to request clarification or add additional context in comments.

5 Comments

yup super. it gives roomnumber array
i was searhing for long time to coimbine arrays, and collapse did it in nice way
There has to be a prettier solution, currently I have code like this: Group::with('service_groups.services')->get()->each->service_groups->each->services->pluck('service_groups')->collapse()->pluck('services')->collapse()->pluck('name'); I know it's just a bit much, but it's basically a many to many to many relationship, so what can you do...
Update: I found a fix and posted it as an answer :)
collapse() is the answer, that just saved my whole life to handle nested relationship. Thank you!
8

You may shorten @Jonas Staudenmeir's answer like so:

$roomnumbers->pluck('floorroomcount.*.roomnumber.*')->collapse();

pluck('*') is essentially the same as collapse() in this particular context.

2 Comments

Is the trailing .* necessary?
@Jonas Staudenmeir I honestly don't know, but actually it probably shouldn't be there at all... Given the collapse(), that is.
1

This is working, but with many loop and echoing directly, if anything can be simplified please let me know :

   $roomnumbers = Room::with(['floorroomcount.roomnumber'])->where('roomtype_id', $roomtype_id)->get();

    $floorroomcounts =  $roomnumbers->pluck('floorroomcount');

    $records =  $floorroomcounts->map(function($floorroomcount, $value){

                    return $floorroomcount->pluck('roomnumber')->flatten();

                })->values()->all();        

    foreach($records as $record){

        foreach($record as $row){
            echo '<option value='.$row->id.'>'.$row->roomnumber.'</option>';
        }

    }

    //return response()->json($roomnumbers);

Comments

0

Try,

$roomnumbers = Room::with(['floorroomcount' => function($query){
    $query->with('roomnumber')->get();
}])
->where('roomtype_id', $roomtype_id)
->get();

$records = $roomnumbers->map(function($element, $value){
    return $element->map(function($e, $v){
        return $e->roomnumber;
    });
})->values()->all();

map() is a Laravel collection method so you need to import the collection facade on the top of the controller like: use Illuminate\Support\Collection;

4 Comments

changed return $element->roomnumber; its not working returning [null,null,null]
just dd($element) and dd($value) inside map() function, what are you getting?
dd($element) gives while room object. and dd($value) gives 0
Try my updated answer. You will might get the nested array of room numbers, you need to flatten it. See laravel.com/docs/5.6/collections#method-flatten
0

In Laravel 5.1 and + you can use flatten() on collection.

method flattens a multi-dimensional collection into a single dimension:

$roomnumbers->flatten()->pluck('floorroomcount');

Comments

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.