I am following a tutorial Articles and comments. Below works fine, but how do i get all comments with the articlename in a foreach?
Model: Article
namespace App\Http\Models;
use Illuminate\Database\Eloquent\Model;
class Article extends Model
{
public function comments() {
return $this->hasMany('App\Http\Models\Comment');
}
protected $fillable = array('title','body');
}
Model: Comments
namespace App\Http\Models;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
public function article() {
return $this->belongs('Article');
}
protected $fillable = array('body','article_id');
}
Example dump in ArticleController:
$items = Article::find(1)->comments()->get();
foreach ($items as $item) {
print_r($item->body);
}