3
我得到一個錯誤不能使用類型爲stdClass的對象作爲數組
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
試試這個哎雷姆,感謝很多的答案,這個問題是清楚了吧:-) – user3483754