简体   繁体   English

Laravel Multi foreach

[英]Laravel multi foreach

I want to get a multi sub comments. 我想获得多条评论。

How can I it don't have to use foreach() { foreach() { } }... 我怎么不用用foreach(){foreach(){}} ...

I have three models: 我有三种模式:

strony.php model strony.php模型

    class Strony extends Model
{
        public function komenty() {
        return $this->belongsToMany('App\Comment', 'taxonomies')->withPivot('parent_id');
    }
    public function sub() {
        return $this->belongsToMany('App\Comment', 'taxonomies', 'parent_id');
    }}

taxonomy.php model taxonomy.php模型

class Taxonomy extends Model
{
    protected $table = 'comments';


    public function tax() 
    {
        return $this->belongsToMany('App\Comment','taxonomies', 'parent_id', 'comment_id')->withPivot('parent_id');
    }
}

comment.php model comment.php模型

class Comment extends Model
{
public function subcomment() {
        return $this->belongsToMany('App\Comment', 'taxonomies', 'parent_id');
    }
    }

controller.php controller.php

echo '<ul>';
foreach($str->komenty as $g) {
$numer = $g->pivot->comment_id;
$child = $g->pivot->parent_id;

if($child == null) {
echo '<li>' . $g->title;

$cmt = Comment::find($numer);

$sub1 = $cmt;

echo '<ul>';
foreach($sub1->subcomment as $sub2) {
    echo '<li>' . $sub2->title . '</li>';

   echo '<ul>';
    foreach($sub2->subcomment as $sub3) {

            echo '<li>' . $sub3->title . '</li>';
            echo '<ul>';
            foreach($sub3->subcomment as $sub4) {
                echo '<li>' . $sub4->title . '</li>';
            }
            echo '</ul>';
    }
    echo '</ul>';
}

echo '</ul>';
echo '</li>';

}
}
echo '</ul>';

table this is a my table sql 这是我的表sql 在此处输入图片说明

-comment1 -comment1

--comment3 --comment3

---comment2 ---评论2

---comment4 ---评论4

----comment6 ----评论6

--comment5 --comment5

 <ul><li>comment1<ul><li>comment3</li><ul><li>comment2</li><ul></ul><li>comment4</li><ul><li>comment6</li></ul></ul><li>comment5</li><ul></ul></ul></li></ul> 

Don't echo HTML code in your controller, use views: https://laravel.com/docs/5.6/views 不要在控制器中回显HTML代码,请使用视图: https : //laravel.com/docs/5.6/views

Create two views and use recursive inclusion. 创建两个视图并使用递归包含。

comment.blade.php : comment.blade.php

<ul>
    @foreach($comment->subcomment as $subcomment)
        <li>{{ $comment->title }}</li>
        @include('comment', ['comment' => $subcomment])
    @endforeach
</ul>

comments.blade.php : comments.blade.php

<ul>
    @foreach($str->komenty as $comment)
        @if(is_null($comment->pivot->parent_id))
            <li>{{ $comment->title }}</li>
            @include('comment', ['comment' => $comment])
        @endif
    @endforeach
</ul>

I try to use 我尝试使用

 foreach($str->komenty as $g) {
$numer = $g->pivot->comment_id;
$child = $g->pivot->parent_id;

if($child == null) {

echo $g->title . '<br>';
$cmt = Comment::find($numer);

$sub2 = $cmt;
$i = 1;
$o = 2;

while($i < 5) {
    $i++;
    $o++;
    if(isset(${'sub'.$i}->subcomment)) {
foreach(${'sub'.$i}->subcomment as ${'sub'.$o}) {
    $num = ${'sub'.$o}->pivot->comment_id;
    echo ${'sub'.$o}->title . '<br>';
}}}}}

It's working only last element. 它仅是最后一个元素。

Because this make foreach(){} foreach(){} , not foreach(){ foreach(){} } 因为这使得foreach(){} foreach(){} ,而不是foreach(){ foreach(){} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM