samedi 4 mai 2019

How to fix "Trying to get property of non-object" Laravel 5

I want to add info from DB different tables in simple blade but when i add variable from another table got error "trying to get property of non-object"

Here is my controller where i output data from 1 table and it gives me a result in blade and its all works ok. i have 20 posts from news in my view. (except paginate)

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\News;
use App\Forum;


class PagesController extends Controller
{
    public function index(){



    $news = News::orderBy('date', 'DESC')->simplePaginate(20);

    return view ('lapas.pamata.index')->with('news', $news);

    /* ,[
        'news'->$news,
        'forum'-> $forum,
    ]); */
}


}

Problem is when i want to add another table information.

Controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\News;
use App\Forum;


class PagesController extends Controller
{
    public function index(){


    $forum = Forum::orderBy('timestamp', 'desc')->get();
    $news = News::orderBy('date', 'DESC')->simplePaginate(20);
    return view ('lapas.pamata.index',[
        'news'->$news,
        'forum'-> $forum,
    ]);
}


}

and blade file:

@extends ('layouts.app')

@section('content')
@if (count($news) > 0)
    @foreach ($news as $new)
        <div class = "col-lg-1 col-md-0">
        </div>
        <div class ="col-lg-5 col-md-12  news-block">
            <div class ="col-12">
                {!!$new->summary!!}
            </div>
        
        </div>
    @endforeach
@endif

@endsection

Now it gives me this error http://prntscr.com/nkb88z

I need to get information from both table.



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2Y6DPdP
via IFTTT

Aucun commentaire:

Enregistrer un commentaire