Laravel 4尝试获取非对象错误的属性

时间:2014-12-02 18:07:51

标签: php laravel laravel-4

我正在我的网站上工作,我退出测试它,它给了我这个错误:

Trying to get property of non-object (View: C:\xampp\htdocs\mom\a\app\views\home.blade.php)

这是我的home.blade.php:

@extends('layout.main')

@section('title') Home @stop

@section('content')
    <!-- User signed in -->
    @if(Auth::check())
        <!-- User is not admin -->
        <p>Welcome back, {{ Auth::user()->username }}.</p>
    @else
        <!-- Nothing -->
    @endif


    @if($posts->count())
        @foreach($posts as $post)
            <article class="col-md-8 col-md-push-2 well well-white home-posts">
                <h2 class="title"><a href="{{ URL::action('post-show', $post->slug) }}">{{ $post->title }}</a></h2>
                <hr class="post-break">
                <h4 class="home-content">{{ Markdown::parse(Str::limit($post->body, 300)) }}</h4>
                <div class="post-footer">
                    <ul class="footer-group">
                        <ul>
                            <li>Published {{ $post->created_at->diffForHumans() }}</li>
                            <li><a href="{{ URL::action('post-show', $post->slug) }}"> Read more &rarr;</a></li>
                        </ul>
                        @if(Auth::user()->group ==1)
                            <ul class="footer-right">
                                <li class="red"><a href="#"><i class="fa fa-trash"></i> Delete</a></li>
                                <li><a href="#"><i class="fa fa-gear"></i> Edit</a></li>
                            </ul>
                        @else
                            <!-- Nothing -->
                        @endif
                    </ul>
                </div>
            </article>
        @endforeach
    @endif
    <div class="col-md-8 col-md-push-2 ">{{ $posts->appends(array())->links() }}</div>
@stop

如果有人可以帮我找出正在调用非对象的属性,并帮我修复它。那太好了。非常感谢。

1 个答案:

答案 0 :(得分:1)

你应该把这一行

<div class="col-md-8 col-md-push-2 ">{{ $posts->appends(array())->links() }}</div>
你的if语句中的

编辑:

尝试更改行:

@if(Auth::user()->group ==1)

@if(Auth::check() && Auth::user()->group ==1)