这是我的需要; 我希望在laravel php框架工作中将不同视图包含在不同视图中。
class DashboardController extends BaseController {
public function comments( $level_1=''){
// process data according to $lavel_1
return View::make('dashboard.comments', $array_of_all_comments);
}
public function replys( $level_2=''){
// process data according to $lavel_1
return View::make('dashboard.replys', $array_of_all_replys);
}
现在可以从
访问这两个数据www.abc.com/dashboard/comments
www.abc.com/dashboard/replys
在我看来,我需要的是根据评论ID生成回复($ lavel_2)
// dashboard/comments.blade.php
@extends('layout.main')
@section('content')
@foreach($array_of_all_comments as $comment)
comment {{ $comment->data }},
//here is what i need to load reply according to the current data;
//need to do something like this below
@include('dashboard.replys', $comment->lavel_2) //<--just for demo
.................
@stop
并在回复中也得到了
@extends('layout.main')
@section('content')
// dashboard/replys.blade.php
@foreach($array_of_all_replys as $reply)
You got a reply {{ $reply->data }},
...........
@stop
有什么方法可以在laravel 4上实现这个目标吗?
请帮助我,我想一次性加载评论和回放,之后需要通过ajax单独访问它们
请提前帮助我,谢谢你
答案 0 :(得分:0)
晕我在这里找到了解决方案
我们只需要使用App::make('DashboardController')->reply();
并从包含视图文件
@extends
和@sections
变化就像这样
// dashboard/comments.blade.php
@extends('layout.main')
@section('content')
@foreach($array_of_all_comments as $comment)
comment {{ $comment->data }},
//<-- here is the hack to include them
{{-- */echo App::make('DashboardController')->reply($comment->lavel_2);/* --}}
.................
@stop
.............
并在回复中现在更改为
// dashboard/replys.blade.php
@foreach($array_of_all_replys as $reply)
You got a reply {{ $reply->data }},
...........
@endforeach
-------------
感谢
答案 1 :(得分:0)
您可能希望重新设计视图并规范化数据。评论和回复(可能)相同。
如果您创建了一个注册模型,其属于“父”(另一个注释模型)并且有多个“子”(许多注释模型),那么只需将parent_id
设置为0以获得顶级注释,然后设置它到另一个评论的ID,使其成为回复。
然后您的Blade视图会执行以下操作:
comments.blade.php
@foreach ($comments AS $comment)
@include( 'comment', [ 'comment' => $comment ] )
@endforeach
comment.blade.php
<div>
<p>{{{ $comment->message }}}</p>
@if( $comment->children->count() )
<ul>
@foreach( $comment->children AS $comment )
<li>
@include( 'comment', [ 'comment' => $comment ] )
</li>
@endforeach
</ul>
@endif
</div>