【问题标题】:Trying to get property 'slug' of non-object 5.7试图获得非对象 5.7 的属性“slug”
【发布时间】:2019-08-02 11:42:32
【问题描述】:

希望解决我在尝试向视图显示一些数据时遇到的这个错误。我正在使用 v5.7,我觉得这可能与我的控制器中的 index 方法有关,我可能错了。如果需要更多信息,请告诉我。

试图获取非对象的属性“slug”(0)

路线:

Route::get('/category/{category}','BlogController@category')->name('category'); 

博客类别模型

namespace App;

use Illuminate\Database\Eloquent\Model;

class BlogCategory extends Model
{
   protected $fillable = ['title', 'slug']; 

   public function posts()
   { 
     return $this->hasMany(Post::class); 
   }

   public function getRouteKeyName()
   { 
     return 'slug'; 
   }
}

后模型

public function category()
{
    return $this->belongsTo(BlogCategory::class);
}

控制器:

protected $limit = 3;

public function index()
{
    $categories = BlogCategory::with(['posts' => function ($query) {
        $query->published();
    }])->orderBy('title', 'asc')->get();

    $posts = Post::with('author')
        ->latestFirst()
        ->published()
        // ->filter(request()->only(['term','year','month']))
        ->simplePaginate($this->limit);

    return view('pages.frontend.blog.index', compact('posts', 'categories'));
}

public function category(BlogCategory $category)
{
    $categoryName = $category->title;

    $categories = BlogCategory::with(['posts' => function ($query) {
        $query->published();
    }])->orderBy('title', 'asc')->get();

    $posts = $category->posts()
        ->with('author')
        ->latestFirst()
        ->published()
        ->simplePaginate($this->limit);

    return view("pages.frontend.blog.index", compact('posts', 'categories', 'categoryName'));
}

查看:

@foreach ($posts as $post)
    <article class="post-item">
        @if ($post->image_url)
            <div class="post-item-image">
                <a href="{{ route('blog.show', $post->slug) }}">
                    <img src="{{ $post->image_url }}" alt="">
                </a>
            </div>
        @endif
        <div class="post-item-body">
            <div class="padding-10">
                <h2>
                    <a href="{{ route('blog.show', $post->slug) }}">{{ $post->title }}</a>
                </h2>
                {!! $post->excerpt_html !!}
            </div>

            <div class="post-meta padding-10 clearfix">
                <div class="pull-left">
                    <ul class="post-meta-group">
                        <li>
                            <i class="fa fa-user"></i>
                            <a href="#"> {{ $post->author->name }} </a>
                        </li>
                        <li>
                            <i class="fa fa-clock-o"></i>
                            <time> {{ $post->date }}</time>
                        </li>
                        <li>
                            <i class="fa fa-folder"></i>
                            <a href="{{ route('category', $post->category->slug) }}"> {{ $post->category->title }}</a>
                        </li>
                        <li>
                            <i class="fa fa-comments"></i>
                            <a href="#">4 Comments</a>
                        </li>
                    </ul>
                </div>
                <div class="pull-right">
                    <a href="{{ route('blog.show', $post->slug) }}">Continue Reading &raquo;</a>
                </div>
            </div>
        </div>
    </article>
@endforeach

发布表格

博客猫表

【问题讨论】:

  • 视图中的 $post->category->slug 应该是 $posts->category->slug
  • 在你的 Post::category 关系函数中,添加-&gt;withDefault();,这将使你的关系回退到一个新的模型而不是 null,以防帖子和类别之间没有关系。
  • 快速回答:-&gt;withDefault(['slug' =&gt; '']);。为什么会这样?你有一个关系缺陷,Laravel 找不到关系(你要么设置错误,要么某个帖子的 category_id 为空,这使得整个事情都属于 foreach)。一个好的做法是在使用之前检查与 if else 的关系... @if ($post-&gt;category) @endif in bladd
  • 您能提供您的帖子和类别表的架构吗?
  • 刚刚写了一个答案,看看吧。

标签: php laravel


【解决方案1】:

belongsTo 函数接受第二个参数作为您的帖子表中的外键名称,如果您不提供它,框架将尝试猜测外键列名称是什么,给出函数名称为模式,在您的情况下为 category(),因此框架正在搜索 category_id,但是,您的外键列名称是 blog_category_id

public function category()
{
    return $this->belongsTo(BlogCategory::class, 'blog_category_id');
}

【讨论】:

  • 我想如果我读完文档就不会发生这种情况了。谢谢!
  • 很高兴我能帮上忙 :)
【解决方案2】:

如果这个视图和index方法相关,你应该在你的控制器的index中调用category关系!

public function index()
{
    $categories = BlogCategory::with(['posts' => function ($query) {
        $query->published();
    }])->orderBy('title', 'asc')->get();

    $posts = Post::with('author')
        ->category()
        ->latestFirst()
        ->published()
        // ->filter(request()->only(['term','year','month']))
        ->simplePaginate($this->limit);

    return view('pages.frontend.blog.index', compact('posts', 'categories'));
}

【讨论】:

    猜你喜欢
    • 2019-01-09
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多