我收到错误
Cannot use object of type stdClass as array on controller
以下是我的控制器
public function showIndex()
{
$data['news'] = News::getAll();
foreach ($data['news'] as $key => $value) {
$data['news'][$key]['category'] = News::getCategory($value->id_berita);
}
return View::make('pages.news.main')
->with('title', 'News')
->with($data);
}
这是我的模型
public static function getAll()
{
$queryResult = DB::table('berita')
->leftJoin('admin', 'berita.post_author', '=', 'admin.id')
->select('id_berita', 'judul', 'post_date', 'aktif', 'fullname', 'post_author')
->orderBy('id_berita', 'desc')
->paginate(3);
return $queryResult;
}
public static function getCategory()
{
$queryResult = DB::table('news_in_category')
->join('menu_website', 'menu_website.id', '=', 'news_in_category.category_id')
->select('menu_website.id', 'title')
->get();
return $queryResult;
}
这是我的视图
@if ($news)
@foreach ($news as $key => $value)
@if ($news->category)
@foreach ($news->category as $key_cat => $cat)
{{ $cat->id }}
@endforeach
@endif
@endforeach
@endif
对于每个新闻都有一个以上的类别,我该如何解决这个问题?请帮我。我正在使用laravel 4
答案 0 :(得分:1)
通过更改
来尝试此操作$data['news'][$key]['category'] = News::getCategory($value->id_berita);
到
$data['news'][$key]->category = News::getCategory($value->id_berita);