0
 $all_display = array();
 $ad_all->each(function($ad){
     $all_display[] =array('num'=>$ad->num);
 });
 print_r($all_display);

alternative

 $all_display = array();
 $ad_all->each(function($ad) use ($all_display){
     $all_display[] =array('num'=>$ad->num);
 });
 print_r($all_display);

$ad_all has four rows, but when I print $all_display, it doesn't display anything.

2
  • did you try var_dump, instead of print? Commented Oct 17, 2017 at 9:40
  • @Eric bro var_dump also prints nothing Commented Oct 17, 2017 at 9:43

2 Answers 2

3

Laravel has envisioned this with a Collection::only method.

var_dump($ad_all->only('num')->toArray());
Sign up to request clarification or add additional context in comments.

Comments

-1

Changing each to foreach works

 $all_display = array(); 
 foreach($ad_all as $ad){
     $all_display[] =array('num'=>$ad->num);
 }

But I don't know why each doesn't work..

1 Comment

I believe that's beacause with each you have to return a value each iteration.

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.