donglanying3855 2014-06-13 07:46
浏览 156
已采纳

laravel:将视图插入数组

I want show dynamic numbers of tickets (views) in my main view (show). I get an array of tickets data from the database. The problem is that laravel don't allow me echo view from view (maybe it possible? and somebody knows how make it) so I create array of tickets views in the controller and pass them to my main view (show). In the main view I use foreach to run over all the views and show them. for some reason laravel throws error Method Illuminate\View\View::__toString() must not throw an exception. it means that something wrong with my views. What am I doing wrong?

My Controller Code:

foreach( $tickets as $ticket ){

$oldTickets[] = View::make('helpers.eventBox', array( 'ticket' => $ticket ));
}

    $layout = View::make('layouts.main');
    $layout->nest('content','profile.show.show',array(

        'oldTickets' => $oldTickets,                
    ));

    return $layout;

Show View:

foreach( $oldTickets as $ticket ){ 

        echo $ticket;                                               
 }

When I pass single view from controller to the view it works. What can my problem be? What is the best solution for it?

Thanks.

EDIT: I had bug in my view. generally it possible to render from view page. I fix my issue.

the blade solution looks like right way too, I try it but don't success, if I will see 5 'up points' near your answer I will mark it as a right question for other users that will have same issue.

sorry and thanks for the help.

  • 写回答

1条回答 默认 最新

  • douji5523 2014-06-13 08:32
    关注

    If I understand your question right, you want to render a particular view for each ticket, and all that nested in a main view.

    So, in controller I would pass all tickets array to a view eg. like:

    return View::make('tickets.list')->with('tickets', $tickets);
    

    And then in view I would call blades render each function, which renders a partial view for all objects in passed array.

    @each('tickets._item', $tickets, 'ticket')
    

    As I've read, it is not documented, but here you can read about it: http://www.neontsunami.com/post/@each-in-laravel-blade

    Haven't checked it, but it should work. I've used render each in blade in Laravel 3 and it worked fine.

    UPDATE: I've checked that it works. This is what I did:

    1. In controller I passed $tickets to a view, like above
    2. Created file .../views/tickets/list.blade.php and inserted there code:

      <ul> @each('tickets._item', $tickets, 'ticket') </ul>

    3. Created file .../views/tickets/_item.blade.php and inserted code:

      <li> {{ $ticket->id }} </li>

    And thats it, all is working fine - for each ticket theres a view rendered defined in _item.blade.php file.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看