Создание приложения в виде миниинстаграммы, где у нас есть рекомендации (посты) и пользователи, которые могут их комментировать.Получение данных через API.И у меня проблема с получением всех комментариев и всех ответов определенного поста.
У меня есть таблица базы данных
comment
-------------------------------
|id |
|text(comment text) |
|recommendation_id(post_id) |
|user_id(author) |
|parent_id(parent comment id) |
-------------------------------
Здесь, в parent_id, мы можем установить идентификатор родительского комментария.Комментарии имеют ответы и ответы также имеют ответы.Вот модель комментария
class Comment extends Model
{
protected $with = ['user:id,name'];
protected $table = 'comments';
protected $fillable = ['text','user_id','recommendation_id'];
public function recommendation()
{
return $this->belongsTo('App\Models\MobileApp\Recommendation' , 'recommendation_id');
}
public function user()
{
return $this->belongsTo('App\User');
}
public function replies()
{
return $this->hasMany('App\Models\MobileApp\Comment' ,'parent_id');
}
public function parent()
{
return $this->belongsTo('App\Models\MobileApp\Comment' ,'id');
}
}
и модель рекомендации
class Recommendation extends Model
{
protected $table = 'recommendations';
protected $fillable = ['course_id','title','description','file'];
public function comments()
{
return $this->hasMany('App\Models\MobileApp\Comment')->where('parent_id', '=', Null);
}
}
А вот индекс метода рекомендации контроллера
public function index()
{
$recommendations = Recommendation::all();
foreach($recommendations as &$item){
//changing to full path
$item['file'] = 'https://example.com/' . $item['file'];
//getting comments
$item['comments']=$item->comments;
//getting replies
foreach($item->comments as $alphabet => $collection) {
$collection->replies;
//if reply array not empty then look for child repliese. I should have here recursive
if($item->comments[$alphabet]->replies->count()>0){
foreach($item->comments[$alphabet]->replies as $alphabet => $collection) {
$collection->replies;
}
}
}
}
if ($recommendations)
{
$response = ['success'=>true, 'data'=>$recommendations];
}
else
$response = ['success'=>false, 'data'=>'Record doesnt exists'];
return $response;
}
Проблема в том, что если я получаю не пустой ответ, то янадо искать ответы детей.У меня должно быть что-то вроде рекурсии.Как я могу это сделать?