donglian1982 2013-09-17 12:32
浏览 83
已采纳

在Laravel 4中使用对象数组

I'm trying to use an array of object between a controller and a view file in Laravel 4.

Controller code :

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');
}

View code "create_sale_add_item" here:

@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

But when I display the corresponding page, I have this error:

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

Does someone has already see this problem?

  • 写回答

2条回答 默认 最新

  • dqvrlgi3247 2013-09-17 16:13
    关注

    I suspect the problem lies in the interaction of Blade's @foreach and the Session class. I think the Session class is unserializing objects every time the loop runs and that seems perilous.

    Try

    $items = Session::get('current_items');
    @foreach ($items as $i)...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥80 部署运行web自动化项目