wobsoriano
wobsoriano

Reputation: 13462

Undefined stdClass::render() Laravel 5.1

I have a simple pagination code and when I try to run it I get the error Call to undefined method stdClass::render(). Here are my codes:

Controller:

public function showNews()
    {
        $news = DB::table('tcity_news')->paginate(1);
        return view('newspage', ['news' => $news]);
    }

Route:

Route::get('/newsroom/city-news', 'TugsiteController@showNews');

Blade:

@foreach ($news as $news)
{{$news->newsTitle}}
@endforeach

{!! $news->render() !!} 

The render() method is undefined, but I am running the latest version of Laravel. Is there something that I am missing?

Upvotes: 0

Views: 3033

Answers (2)

user6138523
user6138523

Reputation: 11

@foreach ($news as $news) for this line laravel is getting your news object which you already converted into loop try to use some different name . Like

@foreach ($news as $new)

and then {!! $news !!} or {!! $news->render() !!}. Now laravel will able to differentiate between the data rendered to your view and data you are converting into loop as object .

hope that will bring joy .

Upvotes: 1

Sami Kuhmonen
Sami Kuhmonen

Reputation: 31193

When you use @foreach ($news as $news) you are basically changing the definition of $news so that it won't be what you expect in the render() part. This is why the function is not found.

Upvotes: 3

Related Questions