24

i have a problem about looping data in controller (laravel 4). my code is like this:

$owner = Input::get('owner');
$count = Input::get('count');
$product = Product::whereOwnerAndStatus($owner, 0)->take($count)->get();

when i want to use foreach to loop for $product result with code like this:

foreach ($product->sku as $sku) {
    // Code Here
}

the result is returning error Undefined property: Illuminate\Database\Eloquent\Collection::$sku

so, i try to improvise a little with this code:

foreach ($product as $items) {
    foreach ($items->sku as $sku) {
        // Code Here        
    }
}

the code returning error like this: Invalid argument supplied for foreach()

is there someone who could help me solve this?

2
  • whereOwnerAndStatus never seen that syntax before. Does that work? Where in the docs is that? Commented Jun 18, 2014 at 5:24
  • How did you pass the $products to your view ? Commented Jun 18, 2014 at 6:10

7 Answers 7

58

This will throw an error:

foreach ($product->sku as $sku){ 
// Code Here
}

because you cannot loop a model with a specific column ($product->sku) from the table.
So, you must loop on the whole model:

foreach ($product as $p) {
// code
}

Inside the loop you can retrieve whatever column you want just adding ->[column_name]

foreach ($product as $p) {
echo $p->sku;
}
Sign up to request clarification or add additional context in comments.

Comments

2

Actually your $product has no data because the Eloquent model returns NULL. It's probably because you have used whereOwnerAndStatus which seems wrong and if there were data in $product then it would not work in your first example because get() returns a collection of multiple models but that is not the case. The second example throws error because foreach didn't get any data. So I think it should be something like this:

$owner = Input::get('owner');
$count = Input::get('count');
$products = Product::whereOwner($owner, 0)->take($count)->get();

Further you may also make sure if $products has data:

if($product) {
    return View:make('viewname')->with('products', $products);
}

Then in the view:

foreach ($products as $product) {
    // If Product has sku (collection object, probably related models)
    foreach ($product->sku as $sku) {
        // Code Here        
    }
}

Comments

2

The view (blade template): Inside the loop you can retrieve whatever column you looking for

 @foreach ($products as $product)
   {{$product->sku}}
 @endforeach

Comments

1

using foreach on table

for me

@foreach($products as $data)
            <tr>
                <td>{{$data->id}}</td>
                <td>{{$data->name}}</td>
                <td>{{$data->price}}</td>
            </tr>
 @endforeach

Comments

0

Is sku just a property of the Product model? If so:

$products = Product::whereOwnerAndStatus($owner, 0)->take($count)->get();

foreach ($products as $product ) {
  // Access $product->sku here...
}

Or is sku a relationship to another model? If that is the case, then, as long as your relationship is setup properly, you code should work.

Comments

0
$allData = Model::where('status', 1)->get();
                foreach ($allData as $data){
                    $data->status = 0;
                    $data->update();
                }

Here every $data is single data and $allData contains full collection.

Comments

0

Edit multiple data in laravel You can use this for updating multiple data in laravel, which i have tested it and it works for me. also remember i used relatationship here

2 Comments

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
And also, please make use of the formatting possibilities we provide. Your code is hardly readable without formatting.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.