1

我正在尝试在 Laravel 4 中的控制器和视图文件之间使用对象数组。

控制器代码:

public function addItem()
{
    $file = Input::file('file'); // your file upload input field in the form should be named 'file'

    $destinationPath = 'uploads/'.str_random(8);
    $filename = $file->getClientOriginalName();
    //$extension =$file->getClientOriginalExtension(); //if you need extension of the file
    $uploadSuccess = Input::file('file')->move($destinationPath, $filename);        

    $item               = new Item();
    $item->name         = Input::get('item_name');
    $item->description  = Input::get('item_description');  
    $item->price        = Input::get('item_price');  
    $item->picture_url  = $destinationPath ."/". $filename;
    $item->id_sale      = Session::get('current_sale')->id;
    $item->save();

    $array_items = Session::get('current_items');
    //$array_items = array($item);
    array_push($array_items, $item);

    //$array_items = array_add($array_items, $item->id, $item);
    Session::put('current_items', $array_items);        

    //print_r(Session::get('current_items'));exit();

    return Redirect::to('create_sale_add_item');
}

在此处查看代码“create_sale_add_item”:

@foreach(Session::get('current_items') as $i)
<div class="row">
    <div class="span2"><h2><img src="{{ $i->picture_url }}" style="width: 200px; height: 150px;"></h2></div>
    <div class="span10">
        <p>Nom : {{ $i->name }}</p>
        <p>Description : {{ $i->description }}</p>
        <p>Prix : {{ $i->price }}</p>
    </div>
</div>
@endforeach

但是当我显示相应的页面时,我有这个错误:

ErrorException

main() [<a href='function.main'>function.main</a>]: The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition &quot;Item&quot; of the object you are trying to operate on was loaded _before_ unserialize() gets called or provide a __autoload() function to load the class definition

有人已经看到这个问题了吗?

4

2 回答 2

1

只需使用以下命令自动加载类:

composer dump-autoload -o

你也不需要使用

Session::put('current_items',$array_items)

一个更整洁的方法是:

return Redirect::to('create_sale_add_item')->with('current_items',$array_items);

你也认为:

@if(Session::has('current_items'))

   @foreach(Session::get('current_items') as $i)
    ...
   @endforeach

@endif
于 2015-01-30T17:31:33.030 回答
-1

我怀疑问题在于 Blade 的 @foreach 和 Session 类的交互。我认为每次循环运行时 Session 类都会反序列化对象,这似乎很危险。

尝试

$items = Session::get('current_items');
@foreach ($items as $i)...
于 2013-09-17T16:13:58.727 回答