laravel错误:尝试获取非对象的属性(\ show.blade.php)

时间:2018-02-23 05:29:46

标签: php laravel web-applications

我正在用laravel制作一个讨论论坛应用,我面对这个错误:

  

尝试获取非对象的属性(查看:   C:\ xampp \ htdocs \ fourm \ resources \ views \ discussion \ show.blade.php)at at   PhpEngine-> evaluatePath(' C:\ XAMPP \ htdocs中\4米\存储\框架\视图/ a2f2c494b8859e0552bfa22c40ceb4065b2efbe5.php&#39 ;,   数组(' __ env' =>对象(工厂),' app' =>对象(应用程序),   '信道' =>对象(收集),'错误' =>对象(ViewErrorBag),   ' d' => null,' best_answer' => null))在CompilerEngine.php(第59行)

这是我的控制器代码:

public function show($slug)
{
     $discussion = Discussion::where('slug', $slug)->first();
     $best_answer = Reply::where('best_answer', 1)->first();
     return view('discussion.show')->with('d', $discussion)->with('best_answer', $best_answer);
}

这是我的观看代码:

@extends('layouts.app')

@section('content')
    <div class="panel panel-default">
        <div class="panel-heading">
            <img src="{{ $d->user->avatar }}" alt="" width="40px" height="40px">&nbsp;&nbsp;&nbsp;
            <span>{{ $d->user->name }}, <b>( {{ $d->user->points }} )</b></span>
            @if($d->is_being_watch_by_user())
                <a href="{{ route('discussion.unwatch', ['id' => $d->id ]) }}" class="btn btn-default btn-xs pull-right">unwatch</a>
            @else
                <a href="{{ route('discussion.watch', ['id' => $d->id ]) }}" class="btn btn-default btn-xs pull-right">watch</a>
            @endif
        </div>

        <div class="panel-body">
            <h4 class="text-center">
                <b>{{ $d->title }}</b>
            </h4>
            <hr>
            <p class="text-center">
                {{ $d->content }}
            </p>

            <hr>

            @if($best_answer)
                <div class="text-center" style="padding: 40px;">
                    <h3 class="text-center">BEST ANSWER</h3>
                    <div class="panel panel-success">
                        <div class="panel-heading">
                            <img src="{{ $best_answer->user->avatar }}" alt="" width="40px" height="40px">&nbsp;&nbsp;&nbsp;
                            <span>{{ $best_answer->user->name }} <b>( {{ $best_answer->user->points }} )</b></span>
                        </div>

                        <div class="panel-body">
                            {{ $best_answer->content }}
                        </div>
                    </div>
                </div>
            @endif
        </div>
        <div class="panel-footer">
                    <span>
                        {{ $d->replies->count() }} Replies
                    </span>
            <a href="{{ route('channel', ['slug' => $d->channel->slug ]) }}" class="pull-right btn btn-default btn-xs">{{ $d->channel->title }}</a>
        </div>
    </div>

    @foreach($d->replies as $r)
        <div class="panel panel-default">
            <div class="panel-heading">
                <img src="{{ $r->user->avatar }}" alt="" width="40px" height="40px">&nbsp;&nbsp;&nbsp;
                <span>{{ $r->user->name }} <b>( {{ $r->user->points }} )</b></span>
                @if(!$best_answer)
                    @if(Auth::id() == $d->user->id)
                        <a href="{{ route('discussion.best.answer', ['id' => $r->id ]) }}" class="btn btn-xs btn-info pull-right">Mark as best answer</a>
                    @endif
                @endif
            </div>

            <div class="panel-body">
                <p class="text-center">
                    {{ $r->content }}
                </p>
            </div>
            <div class="panel-footer">
                @if($r->is_liked_by_user())
                    <a href="{{ route('reply.unlike', ['id' => $r->id ]) }}" class="btn btn-danger btn-xs">Unlike <span class="badge">{{ $r->likes->count() }}</span></a>
                @else
                    <a href="{{ route('reply.like', ['id' => $r->id ]) }}" class="btn btn-success btn-xs">Like <span class="badge">{{ $r->likes->count() }}</span></a>
                @endif
            </div>
        </div>
    @endforeach

    <div class="panel panel-default">
        <div class="panel-body">
            @if(Auth::check())
                <form action="{{ route('discussion.reply', ['id' => $d->id ]) }}" method="post">
                    {{ csrf_field() }}
                    <div class="form-group">
                        <label for="reply">Leave a reply...</label>
                        <textarea name="reply" id="reply" cols="30" rows="10" class="form-control"></textarea>
                    </div>

                    <div class="form-group">
                        <button class="btn pull-right">Leave a reply</button>
                    </div>
                </form>
            @else

                <div class="text-center">
                    <h2>Sign in to leave a reply</h2>
                </div>
            @endif
        </div>
    </div>
@endsection

4 个答案:

答案 0 :(得分:1)

您收到此错误,因为两个查询都返回null

$discussion = Discussion::where('slug', $slug)->first();
$best_answer = Reply::where('best_answer', 1)->first();

因此,您需要使用is_null()empty()手动检查空结果,或者只需:

if (!$discussion) {
    abort(404);
}

如果没有找到记录,请使用findOrFail()抛出异常。

答案 1 :(得分:0)

您正在尝试使用null对象访问属性,这就是您遇到“尝试获取null对象的属性”这一错误的原因。

根据我们在评论中的讨论,您在两个查询中都获得null值,这就是出错的原因!

尝试添加if..else条件,如下所示:

if (!$d) {   
     session()->flash('error', 'Discussion not found.'); 
     return redirect()->back(); 
} else {
    return view('discussion.show')->with('d', $discussion)->with('best_answer', $best_answer);
}

希望这能帮到你!

答案 2 :(得分:0)

确保您没有返回对象并将其作为数组访问。这可能是常见的错误。

答案 3 :(得分:-1)

我通过在出现错误的页面上添加:<?php error_reporting(0);?> 解决了我的问题。 :D