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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀