【问题标题】:Cant show related variable in Laravel blade无法在 Laravel 刀片中显示相关变量
【发布时间】:2016-10-01 22:13:45
【问题描述】:

我无法在 laravel Blade 中显示相关变量

public function GetAll()
{
 $news=DB::table('news')->get();
 return View('index',['news'=>$news]);
}

在视图中:

@foreach($news as $new)
 ...
        <a href="#">{{$new->comments()->count()}} Comments</a>
 ...
@endforeach

它也不适用于对象的任何变量但对第一项效果很好

public function Count()
{
   $news=News::find(1);
  echo $news->comments()->count();
 }

【问题讨论】:

  • 它适用于第一项,因为您使用 ORM(“好”方式),而使用 DB::table 时,您获取的不是对象而是数组。所以使用@Alex 解决方案。

标签: php laravel orm eloquent blade


【解决方案1】:
public function GetAll()
{
$news = News::with('comments')->get();
 return View('index',['news'=>$news]);
}

使用 ORM 代替 DB。

【讨论】:

    猜你喜欢
    • 2016-12-09
    • 1970-01-01
    • 2016-08-18
    • 2016-11-21
    • 1970-01-01
    • 1970-01-01
    • 2015-02-17
    • 1970-01-01
    • 2020-08-21
    相关资源
    最近更新 更多