简体   繁体   English

在if else循环进入后帖子无法正确显示

[英]Posts not displaying properly after if else loop put in

2 problems: 2个问题:

1) the Recent Posts function in my view file (for the single post page) no longer shows posts. 1)我的视图文件(对于单个帖子页面)中的“最近的帖子”功能不再显示帖子。
2) On the articles page which shows all of the posts, the posts with no slug do not work. 2)在显示所有帖子的文章页面上,没有的帖子不起作用。 when I click on the post, it redirects to /articles. 当我单击该帖子时,它重定向到/ articles。 Only posts with a slug work. 只发布有工作的帖子。

I have 2 kinds of URLs for my posts: slug and url. 我的帖子有两种URL:slug和url。 The difference? 区别? URL is longer. 网址更长。 Slug is shorter and used for SEO purposes. 子弹头较短,用于SEO。 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. 1)我的视图文件(对于单个帖子页面)中的“最近的帖子”功能不再显示帖子。

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(); 这只会返回您的查询生成器对象,因此要获取这些帖子,您需要添加-> 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): 之后,您可以通过(-> first())过滤集合,以过滤出您要查找的帖子,因为它不支持orWhere方法(或者像您在旧代码中那样仅使用两个where()) :

$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. 2)在显示所有帖子的文章页面上,没有的帖子不起作用。 when I click on the post, it redirects to /articles. 当我单击该帖子时,它重定向到/ 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. 如果你看看你with你逝去的两个postposts ,因此它可能与混合环状变量with变量。

Try and change the keys of the variables in the with to something more distinct. 尝试将with变量的键更改为更独特的键。 You could add an _ before the single $post variable. 您可以在单个$post变量之前添加_

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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