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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器