单击第二页的第二篇文章时出现错误

时间:2019-03-15 05:36:24

标签: laravel-5 laravel-5.2 laravel-5.1

当我尝试在单页中打开第一篇文章时,它正在打开;当我试图在单页中打开第二篇文章时,它显示“正在尝试获取非对象的属性'title'”

这是代码

FrontendController

public function singlePost($slug)
{

    $post= Post::where('slug', $slug)->first();


    return view('single')->with('post', $post)


        ->with('title', $post->title)
        ->with('settings', Setting::first())
        ->with('categories', Category::take(4)->get());

}

single.blade.php

在同一页面上使用相同的前端控制器 @extends('layouts.frontend')

@section('content')

<div id="product-post">
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <div class="heading-section">

                    <img src="{{$post->featured}}" alt="" />
                </div>
            </div>
        </div>
        <div id="single-blog" class="page-section first-section">
            <div class="container">
                <div class="row">
                    <div class="product-item col-md-12">
                        <div class="row">
                            <div class="col-md-8">

                                <div class="product-content">
                                    <div class="product-title">
                                        <h3>{{$post->title}}</h3>
                                        <span class="subtitle">4 comments</span>
                                    </div>
                                    <p>
                                        {!! $post->content!!}

                                    </p>
                                </div>



                                <div class="leave-form">
                                    <form action="#" method="post" class="leave-comment">
                                        <div class="row">
                                            <div class="name col-md-4">
                                                <input type="text" name="name" id="name" placeholder="Name" />
                                            </div>
                                            <div class="email col-md-4">
                                                <input type="text" name="email" id="email" placeholder="Email" />
                                            </div>
                                            <div class="subject col-md-4">
                                                <input type="text" name="subject" id="subject" placeholder="Subject" />
                                            </div>
                                        </div>
                                        <div class="row">
                                            <div class="text col-md-12">
                                                <textarea name="text" placeholder="Comment"></textarea>
                                            </div>
                                        </div>
                                        <div class="send">
                                            <button type="submit">Send</button>
                                        </div>
                                    </form>
                                </div>
                            </div>

                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>




@endsection

1 个答案:

答案 0 :(得分:0)

在获取确切的列值之前,必须检查该值是否来自表。在这种情况下,如果表返回空结果,则可以将其重定向到404页。

请参考下面的代码:

public function singlePost($slug)
{

   $post= Post::where('slug', $slug)->first();

   if($post) {
     return view('single')->with('post', $post)
       ->with('title', $post->title)
       ->with('settings', Setting::first())
       ->with('categories', Category::take(4)->get());
   } else {
      // You can redirect to 404 page
   }

}