简体   繁体   中英

Posts not displaying properly after if else loop put in

2 problems:

1) the Recent Posts function in my view file (for the single post page) no longer shows posts.
2) On the articles page which shows all of the posts, the posts with no slug do not work. when I click on the post, it redirects to /articles. Only posts with a slug work.

I have 2 kinds of URLs for my posts: slug and url. The difference? URL is longer. Slug is shorter and used for SEO purposes. Why not use just slug? Because for some posts, I don't want to waste time creating a slug. The data in the slug column for those posts is blank/empty. The generated url is fine. For other posts, I want a slug made.

Here is my controller code for posts.

Old version:

public function viewpost($url,$slug){
if ( empty($slug) ) {
$url ='articles/'.$url;
}  else {
$url = 'articles/'.$slug;

}

$posts = posts::where('post_status', '')->get();
if ( empty($slug) ) {
$post = $posts->where('url', $url)->first();
}  else {
$post = $posts->where('slug', $slug)->first();
}

if ( empty($post) )
return redirect()->back();


return view('viewpost')->with('post', $post)->with('posts', $posts);
}

New version (modified):

public function viewpost($url){


  $posts = posts::where('post_status', '');
  $post = $posts->where('url', $url)->orWhere('slug' , $url)->first();


  if ( empty($post) )
return redirect()->back();


return view('viewpost')->with('post', $post)->with('posts', $posts);
}

The new modified controller file broke the posts without a slug.

Here is the view file for the Recent Posts which broke:

<h3>Recent Posts</h3>
 @foreach($posts as $post)
 <p><a href="{{ URL::to($post->url) }}"><img style="width:100px;" src="{{asset('thumbnails/'.$post->thumbnail)}}" class="responsive"></a><br>
     <a href="{{ URL::to($post->url) }}">{{substr(($post->title),0,88)}}.. 
 </a></p>               
  @endforeach

My Routes code:

Route::get('articles/{url}', 'postsController@viewpost');

My view file code for the posts page which displays all the posts:

                    @foreach($post as $post)

                    <div class="col-md-4">

                        <div class="content">
                        @if(!empty($post->slug))
                            dd(11);
                        <a href="{{'articles/'.$post->slug}}" class="latest-heading">{{substr(($post->title),0,88)}}..</a>

                        @else
                        dd(22);
                        <a href="{{($post->url)}}" class="latest-heading">{{substr(($post->title),0,88)}}..</a>
                        @endif
                            <img style="padding: 5px; width:100%; height:218px;" src="{{asset('thumbnails/'.$post->thumbnail)}}" class="img-responsive">

                        </div>

                    </div>

                   @endforeach

1) the Recent Posts function in my view file (for the single post page) no longer shows posts.

That's because unlike your old code, you're not retrieving the data:

$posts = posts::where('post_status', '');

This will only return your query Builder object, so to get those posts, you need to add ->get(); to get the collection, so the code becomes:

$posts = posts::where('post_status', '')->get();

After that you can filter the one post your looking for via (->first()), by filtring the collection, as it doesn't support the orWhere method (or just use two where() as you did in the old code):

$post = $posts->filter(function($object, $key) use ($url) {
     return !strcmp($object->url, 'articles/'.$url) or !strcmp($object->slug, $url);
})->first();

Also correct that:

@foreach($post as $post)

2) On the articles page which shows all of the posts, the posts with no slug do not work. when I click on the post, it redirects to /articles. Only posts with a slug work.

I'm not sure but what i saw in your code, and basing on the old method you showed above, I think you have a problem here:

<a href="{{'articles/'.$post->slug}}" ...
<a href="{{($post->url)}}" ...

doesn't the second one needs also to be (?):

<a href="{{'articles/'.$post->url}}" ...

This is probably the causing factor:

return view('viewpost')->with('post', $post)->with('posts', $posts);

and your loop is doing:

@foreach($posts as $post)

and your other loop is doing:

@foreach($post as $post)

If you look at your with you are passing both post and posts , as a result it is probably mixing the looped variable with the with variable.

Try and change the keys of the variables in the with to something more distinct. You could add an _ before the single $post variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM