1

I am trying to loop in an array but when it comes to the query it gets only the first element, so a little help would be very important.

 $data = Offers::whereIn('id_business', $business_id_array)
                    ->where([
                        'visible' => 'yes',
                        'delete' => 'no'
                    ])
                    ->where('end_date', '>=', date('Y-m-d h:m:i'))
                    ->orderBy('id', 'desc')
                    ->get();

                $data=array($data);

                foreach($data as $key => $item) {
                    $offers = DB::select('the data i need to get WHERE o.`id` = ' . $item[$key]['id']);


                }

and this is my problem in here, It gets only the id of the first element

o.`id` = ' . $item[$key]['id']
2
  • Try $item->id in your loop. Commented Jun 9, 2017 at 14:13
  • @AmitGupta no man, it doesnt work Commented Jun 9, 2017 at 14:21

3 Answers 3

1

because you have return the view in side the foreach loop, so it only loop through the first item and return. What you can do with this case is

$data = Offers::whereIn('id_business', $business_id_array)...->get()->toArray();
$offers = array_map(function($item){
     $offer = DB::select('the data i need to get WHERE o.`id` = ?', [$item->id]);  
     return $offer;
},$data);
return view(....,['offers' =>$offers]);
Sign up to request clarification or add additional context in comments.

Comments

0

First, you do not need to cast the $data to an array - it will get returned as a collection, which you can iterate through like an array. So you'll be able to use something like this

$offers = Offers::whereIn('id_business', $business_id_array)...->get();

foreach ($offers as $offer) {
    $moreData = DB::select('the data i need to get WHERE o.`id` = ?', [$offer->id]);
}

11 Comments

it still get only one id, only the first one, while i get 4 elements as array
use foreach ($offers->items as $offer) instead
@ayip you mean foreach ($offers as $items=>$offer)
no, use either foreach ($offers->items as $offer) or if it gives any errors, use foreach ($offers->getItems() as $offer)
@User154584 do you do anything with $moreData inside the loop beside running the query?
|
0

This looks like you are using the id from one table, to get the associated data from another table.

Should there not be a relationship in place, between the two tables?

The answer from @Chris G looks correct, maybe dd($offers) to be certain what is in there.

Mick

2 Comments

it get an array when like this : Collection {#256 ▼ #items: array:4 [▼ 0 => Offers {#255 ▶} 1 => Offers {#254 ▶} 2 => Offers {#253 ▶} 3 => Offers {#252 ▶} ] }
and inside each of this arrays I need to get the ID

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.