User45648
User45648

Reputation: 103

Array data issue Laravel

I have an array issue in here and I'm spending a lot of time with. I get a list of array like this form

array:4 [

0 => array:20 [▼
"id" => 58
"id_business" => 
"id_branch" => 
"affected_branches" => " " ▶"
"name" => ""
"banner" => ""
"description" => ""
"url" => 
"start_date" => ""
"end_date" => ""
"nr_pages" => 4
"nr_products" => 0
"type" => "global"
"views" => 24
"visible" => "yes"
"delete" => "no"
"user_create_id" => 
"user_modify_id" => 
"created_at" => ""
"updated_at" => "2017-06-07 14:19:23"]
]

by the following code

  $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()
                    ->toArray();

when i try to make a foreach loop for all arrays that came, in this case 4, it does not return me 4 values, but only one. The loop is:

 foreach ($data as $key => $slide)
                {
                    $offers = DB::select('SELECT o.`id`... WHERE o.`id` = ?',[$slide['id']]);

                }

                return view('...', ['offers' => $offers]);
            }

I pass this values in query to get the id of each of arrays

o.`id` = ?',[$slide['id']]

PS: PLS Help Me :)

Upvotes: 0

Views: 41

Answers (2)

B. Desai
B. Desai

Reputation: 16436

You need to change $offres in to array

$offers = array();
foreach ($data as $key => $slide)
{
    $offers[] = DB::select('SELECT o.`id`... WHERE o.`id` = ?',[$slide['id']]);

}

return view('...', ['offers' => $offers]);

Upvotes: 1

Alexey Mezenin
Alexey Mezenin

Reputation: 163748

You're overwriting $offers value with each iteration, so change your code to:

$offers = [];
foreach ($data as $key => $slide) {
    $offers[] = DB::select...
}

Upvotes: 2

Related Questions