Php 试图在单个post页上获取非对象错误的属性

Php 试图在单个post页上获取非对象错误的属性,php,laravel,laravel-5,Php,Laravel,Laravel 5,我有一个文章页面,显示我网站上每篇文章的循环。然后我有一个单独的贴子页面显示文章本身。我正试图修改文章页面,以便最近的文章在侧边栏上以循环方式显示。我试图将循环代码从文章页面复制到我的帖子页面,但我遇到了一个错误 文章页面代码(无错误): @如果(isset($_请求['searchItem'])&&&$_请求['searchItem']'') @如果(isset($msg)) {{$msg}} @否则 @foreach($result作为$result) 缩略图)}}“class=”im

我有一个文章页面,显示我网站上每篇文章的循环。然后我有一个单独的贴子页面显示文章本身。我正试图修改文章页面,以便最近的文章在侧边栏上以循环方式显示。我试图将循环代码从文章页面复制到我的帖子页面,但我遇到了一个错误

文章页面代码(无错误):


@如果(isset($_请求['searchItem'])&&&$_请求['searchItem']'')
@如果(isset($msg))
  • {{$msg}}
@否则 @foreach($result作为$result) 缩略图)}}“class=”img responsive“> @endforeach @恩迪夫 @否则 @foreach($post as$post) 缩略图)}}“class=”img responsive“> @endforeach @恩迪夫
单个帖子页面的代码:

  <div class="container layer1">
    @if(isset($_REQUEST['searchItem']) && $_REQUEST['searchItem']<>'')
                    @if(isset($msg))

                    <div class="alert alert-danger">

                         <ul>

                            <li><h3>{{ $msg }}</h3></li>

                         </ul>

                    </div>

                    @else

    <div class="row"  style="width:25%;float:left;padding-left:5px;">

        <div class="post-content">

            <div class="post-container">

                <h1>{{ $post->title }}</h1>

                <p style="color:#888;">{{ date ('M-d-Y',strtotime($post->created_at)) }}</p>

       <!--         <p><img src="{{asset('thumbnails/'.$post->thumbnail)}}" class="responsive"></p> -->

                <p><?php echo html_entity_decode($post->content); ?></p>

            </div>

        </div>

    </div>
    <div class="row" style="width:25%;float:left;padding-left:5px;">

        <div class="post-content">

            <div class="post-container">
                <div class="container articles-page">

                    <div class="articles-title">

                    </div>      
                    <div class="container">
                        <h3>Recent Posts</h3>
                     @foreach($result as $result)
                    <div class="col-md-4">

                        <div class="content">

                        <a href="{{$result->url}}" class="latest-heading">{{substr(($result->title),0,88)}}..</a>

                            <img style="padding: 5px; width:100%; height:218px;" src="{{asset('thumbnails/'.$result->thumbnail)}}" class="img-responsive">

                        </div>

                    </div>

                    @endforeach

                    @endif
                    @else
                         @foreach($post as $post)
 error is here =>      <p><a href="{{ $post->url }}"><img style="width:100px;" src="{{asset('thumbnails/'.$post->thumbnail)}}" class="responsive"></a><br>
                        <a href="{{$post->url}}">{{substr(($post->title),0,88)}}..</a></p>
                      </tr>
                   @endforeach
                   @endif
                    <div class="row col-md-12">
                    <div class="panel-body">

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

</div>

@如果(isset($_请求['searchItem'])&&&$_请求['searchItem']'')
@如果(isset($msg))
  • {{$msg}}
@否则 {{$post->title}

{{date('M-d-Y',strottime($post->created#at))}

最近的职位 @foreach($result作为$result) 缩略图)}}“class=”img responsive“> @endforeach @恩迪夫 @否则 @foreach($post as$post) 错误在这里=>

@endforeach @恩迪夫
错误在这一行:

                      <p><a href="{{ $post->url }}"><img style="width:100px;" src="{{asset('thumbnails/'.$post->thumbnail)}}" class="responsive"></a><br>

控制器文件中的代码:

//view post

public function viewpost($url){

  $url ='articles/'.$url;

    if(count(posts::where('url', '=', $url)->first())<>1){

        return redirect()->back();

    }

    return view('viewpost')

    ->with('post', posts::where('url', '=', $url)->first());

}

    //all posts route

    public function allposts(){

    $post=posts::All();

      if(count($post)==0){

        return redirect()->back()->with('message', 'There are no posts at this moment');

      }

      else{

        return view ('admin-SchoolDir.allposts',array('post' => $post))->with("title","Admin-Allposts");

      }

    }
//查看帖子
公共函数viewpost($url){
$url='articles/'.$url;
if(count(posts::where('url','=',$url)->first())1){
返回重定向()->back();
}
返回视图('viewpost')
->使用('post',posts::where('url','=',$url)->first());
}
//所有邮政路线
公共职能所有职位(){
$post=posts::All();
如果(计数($post)=0){
return redirect()->back()->带有('message','此时没有帖子');
}
否则{
返回视图('admin-SchoolDir.allposts',数组('post'=>$post))->带有(“title”,“admin allposts”);
}
}

要将viewpost方法中定义的post用作视图中的数组时,它是一个对象

您的代码必须是这样的

 public function viewpost($url){

   $url ='articles/'.$url;

   if(count(posts::where('url', '=', $url)->first())<>1){
      return redirect()->back();
    }

   return view('viewpost')->with(['post'=>posts::where('url', '=', $url)->first(),'posts'=>posts::where('url', '=', $url)->get()]);
 }
 public function viewpost($url){

      $url ='articles/'.$url;

      if(!posts::where('url', '=', $url)->first()){
       return redirect()->back();
      }

     return view('viewpost')->with(['post'=>posts::where('url', '=', $url)->first(),'posts'=>posts::where('url', '=', $url)->get()]);
 }
编辑:
您不需要使用countoversingleobject来检查是否存在您的代码应该是这样的

 public function viewpost($url){

   $url ='articles/'.$url;

   if(count(posts::where('url', '=', $url)->first())<>1){
      return redirect()->back();
    }

   return view('viewpost')->with(['post'=>posts::where('url', '=', $url)->first(),'posts'=>posts::where('url', '=', $url)->get()]);
 }
 public function viewpost($url){

      $url ='articles/'.$url;

      if(!posts::where('url', '=', $url)->first()){
       return redirect()->back();
      }

     return view('viewpost')->with(['post'=>posts::where('url', '=', $url)->first(),'posts'=>posts::where('url', '=', $url)->get()]);
 }

发布您的控制器代码。我可以看到您正在使用类似于Laravel的东西,但是,您是否正在将$Post参数传递给新视图?@Salama96-done,code posted@Asfo-它是Laravel。我想我是。检查代码。检查答案@ThomasI在返回视图行上出现错误:解析错误:语法错误,意外的“=>”Nevermind,复制了您的co大错特错。现在问题是……整篇文章都不见了。我看到的只是我最近的一个页面上的缩略图。她的eis是一个实时帖子的例子:我编辑了代码,因为似乎没有符合您条件的帖子,所以它将重定向回代码检查如果(!posts::where('url','=',$url)->first())