3

I get an error

Cannot use object of type stdClass as array on controller

Below is my 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);
}

This is my model

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;
}

and this is my view

@if ($news)
    @foreach ($news as $key => $value)

       @if ($news->category)

           @foreach ($news->category as $key_cat => $cat)
               {{ $cat->id }}
           @endforeach

       @endif

    @endforeach
@endif

For every news have one on more than one category, How can I fix this problem ? Please help me. I am using laravel 4

1 Answer 1

1

try this by changing

$data['news'][$key]['category'] = News::getCategory($value->id_berita);

to

$data['news'][$key]->category = News::getCategory($value->id_berita);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.