laravel

用多型方式

comments

id ,commentable_type,commentable_id

1                

2      Article                         1

3      Comment           1

 

Comment Modle

function replys(){

return $this->morphMany('App\Comment,'commentable',')

}

所以渲染時

 

$article=Article::find(1)

在blade

@foreach($article->comments() as $comment)

   

   @foreach($comment->replys() as $reply)

---------------------

當spa時要做

Article::with('comments','comments.replys')->find(1)

{comments:[

{

replys:[

{        }

]

}]

 

-------當parent_id

id parent_id   article_id

1                         1

2     1

Comment Modle

function replys(){

return $this->hasMany('App\Comment,'parent_id',');

}

渲染時

@foreach($aritcle->comments as $comment

@foreach($comment->replies

 

spa會長

Article::with('comments','comments.replies)

 

-------------

多型時可用trait減少代碼

 

 

-------------

 

 

 

 

 

arrow
arrow
    全站熱搜

    學習程式 發表在 痞客邦 留言(0) 人氣()