【问题标题】:ErrorException Undefined variable: posts [duplicate]ErrorException未定义的变量:帖子[重复]
【发布时间】:2020-03-20 20:02:57
【问题描述】:

我想在几个不同的表中获取数据库中的一些字段数据,并且我已经在 post 模型中的表之间建立了关系,并且在 ListingPageController 我已经分配了 post 模型并创建了名为 $posts 的变量以在 list.blade.php 中使用它当我尝试使用它时页面会给我这个错误请帮助

以下代码是ListingPageController页面:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Post;

class ListingPageController extends Controller
{
    public function index(){
        return view('front.listing');
    }

    public function listing($id){ 

         $posts = Post::with(['comments','category','creator'])
                      ->where('status',1)
                      ->where('category_id',$id)
                      ->orWhere('created_by',$id)
                      ->orderBy('id','DESC')->paginate(3); 

       return view('front.listing',compact('posts'));
  }


}

下面的代码是listing.blade.php页面

@foreach($posts as $key=>$post)
                @if($key === 0)

<div class="entity_wrapper">
    <div class="entity_title header_purple">
        <h1><a href="{{ url('/category') }}/{{ $post->category_id }}">{{ $post->category->name }}</a></h1>
    </div>
    <!-- entity_title -->

    <div class="entity_thumb">
        <img class="img-responsive" src="{{ asset('post') }}/{{ $post->main_image }} " alt="{{ $post->title }}">
    </div>
    <!-- entity_thumb -->

    <div class="entity_title">
        <a href="{{ url('/details') }}/{{ $post->slug }}" target="_blank"><h3> {{ $post->title }} </h3></a>
    </div>
    <!-- entity_title -->

    <div class="entity_meta">
        <a href="#">{{ date('F j- Y',strtotime($post->created_at)) }}</a> , by: <a href="{{ url('/author') }}/{{ $post->creator->id }}">{{ $post->creator->name }} </a>
    </div>
    <!-- entity_meta -->

    <div class="entity_content">
        {{ str_limit( $post->short_description,200,'...' )  }}
    </div>
    <!-- entity_content -->

    <div class="entity_social">

        <span><i class="fa fa-comments-o"></i>{{ count($post->comments) }} <a href="#"> Comments</a></span>
    </div>
    <!-- entity_social -->

</div>
<!-- entity_wrapper -->
      @else
    @if($key === 1)
<div class="row">
    @endif <!-- first if will be ended here -->
    <div class="col-md-6" style="min-height: 555px;margin-bottom:2%">
        <div class="category_article_body">
            <div class="top_article_img">
                <img class="img-fluid" src="{{ asset('post') }}/{{ $post->list_image }}" alt="{{ $post->title }}">
            </div>
            <!-- top_article_img -->

            <div class="category_article_title">
                <h5>
                    <a href="{{ url('/details') }}/{{ $post->slug }}" target="_blank"> {{ $post->title }} </a>
                </h5>
            </div>
            <!-- category_article_title -->

            <div class="article_date">
                <a href="#">{{ date('F j- Y',strtotime($post->created_at)) }}</a> , by: <a href="{{ url('/author') }}/{{ $post->creator->id }}">{{ $post->creator->name }}</a>
            </div>
            <!-- article_date -->

            <div class="category_article_content">
               {{ str_limit( $post->short_description,100,'...' )  }}
            </div>
            <!-- category_article_content -->

            <div class="article_social">

                <span><i class="fa fa-comments-o"></i>{{ count($post->comments) }} <a href="#"> Comments</a></span>
            </div>
            <!-- article_social -->

         </div>
        <!-- category_article_body -->

        </div>
    <!-- col-md-6 -->
    @if($loop->last)

    </div>
      @endif
        @endif
         @endforeach

【问题讨论】:

  • 控制器使用front.listing文件,但是你给我们看list.blade.php,不是同一个文件。
  • 抱歉我写错了页面已经命名为listing
  • 哪个文件/行是错误?如果是视图,可以在storage/framework/views中获取解析代码
  • 你可以用-&gt;where(['status' =&gt; 1, 'category_id' =&gt; $id])-&gt;...替换-&gt;where('status',1)-&gt;where('category_id',$id)
  • @ledisoft 它会给出同样的错误

标签: php laravel laravel-5 laravel-5.8


【解决方案1】:

索引函数没有定义posts 变量。您需要更新您的视图或传递一个空变量帖子:

public function index(){
    return view('front.listing', ['posts'=>[]]);
}

或者在你看来:

@if(isset($posts))
    @foreach($posts as $key=>$post)
    ...
@endif

或者创建一个新的视图,改变逻辑等等,这取决于你。

【讨论】:

  • 我更喜欢为 posts 传递一个空变量。谢谢
【解决方案2】:

你可以试试看它告诉你什么吗?

return view('front.listing', ['posts' => $posts]);

EDIT1

验证您传递给listing.blade.php 的内容是否正确......在listing.blade.php 的第一行输入{{ dd($posts) }}

并检查您得到的结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 2018-01-26
    • 2020-09-24
    • 2018-11-29
    相关资源
    最近更新 更多