为foreach()提供的参数无效

时间:2014-09-09 15:21:54

标签: php laravel laravel-4

我正在逐步学习laravel4, 我多次使用该代码,但它是第一次给我这个错误,这是我的控制器代码部分

public function getIndex () {
    $categories = array();


    foreach ( Category::all() as $key=> $category) {
        $categories[$category->id] = $category->name ;
    }


    return View::make('products.index')
    ->with('products' , Product::all())
    ->with('categories' , $categories);
 }

这是代码的视图部分

<ul>    @if(is_array($products))

    @foreach ($products as $product)
        <li>
            {{HTML::image($product->image , $product->title , array('width'=>'50'))  }}
            {{ $product->title }} - 
            {{ Form::open(array('url'=>'admin/products/destroy')) }}
            {{ Form::hidden ('id' , $product->id) }}
            {{ Form::submit('delete') }}
            {{ Form::close() }}


            {{ Form::open(array('url'=>'admin/products/toggle-availability'))}}
            {{ Form::hidden ('id', $product->id)}}
            {{ Form::select('availability' , array('1'=>'In stock' , '0'=>'out of stock') , $product->availability) }}
            {{ Form::submit ('update') }}
            {{ Form:: close() }}
        </li>
@endforeach 
    @endif

添加

控制器中的

dd(Product::all()); ..这就是我得到的

>     object(Illuminate\Database\Eloquent\Collection)#170 (1) { ["items":protected]=> array(1) { [0]=> object(Product)#168 (20) {
> ["fillable":protected]=> array(6) { [0]=> string(11) "category_id"
> [1]=> string(5) "title" [2]=> string(11) "description" [3]=> string(5)
> "price" [4]=> string(12) "availability" [5]=> string(5) "image" }
> ["connection":protected]=> NULL ["table":protected]=> NULL
> ["primaryKey":protected]=> string(2) "id" ["perPage":protected]=>
> int(15) ["incrementing"]=> bool(true) ["timestamps"]=> bool(true)
> ["attributes":protected]=> array(9) { ["id"]=> int(0)
> ["category_id"]=> int(1) ["title"]=> string(4) "test"
> ["description"]=> string(4) "test" ["price"]=> string(2) "22"
> ["availability"]=> int(0) ["image"]=> string(0) "" ["updated_at"]=>
> string(10) "0000-00-00" ["created_at"]=> string(10) "0000-00-00" }
> ["original":protected]=> array(9) { ["id"]=> int(0) ["category_id"]=>
> int(1) ["title"]=> string(4) "test" ["description"]=> string(4) "test"
> ["price"]=> string(2) "22" ["availability"]=> int(0) ["image"]=>
> string(0) "" ["updated_at"]=> string(10) "0000-00-00" ["created_at"]=>
> string(10) "0000-00-00" } ["relations":protected]=> array(0) { }
> ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) {
> } ["appends":protected]=> array(0) { } ["guarded":protected]=>
> array(1) { [0]=> string(1) "*" } ["dates":protected]=> array(0) { }
> ["touches":protected]=> array(0) { } ["observables":protected]=>
> array(0) { } ["with":protected]=> array(0) { }
> ["morphClass":protected]=> NULL ["exists"]=> bool(true) } } }

1 个答案:

答案 0 :(得分:0)

尝试更改

return View::make('products.index')
->with('products' , Product::all())
->with('categories' , $categories);

$product = new Product;
$products = $product->get();

return View::make('products.index'), array(
  'products' => $products,
  'categories' => $categories
);