Laravel 5.7 分页的多对多关系问题

Laravel 5.7 Many To Many Relation Problem With Pagination

我对 laravel 中的这种关系有一些疑问。 this is my mysql tables

这是我的控制器代码:

namespace App\Http\Controllers\Dynamic;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Blogs;
use LaravelLocalization;
use App\BlogsCategory;


class BlogsController extends Controller
{
    public function main(){
        //$posts = new Blogs::where('lang',LaravelLocalization::getCurrent);
        $posts = new Blogs;
        $data = $posts::where('lang',LaravelLocalization::getCurrentLocale())->where('created_at','<=',NOW())->orderBy('created_at','DESC')->paginate(config('PAGINATOR_COUNTER'));
        return view('Dynamics.Blogs.Main',compact('data'));
    }
}

这是我的博客模型:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\BlogsCategory;

class Blogs extends Model
{
    public function categories()
    {
        return $this->belongsToMany(BlogsCategory::class);
    }
}

和博客类别模型:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\Blogs;

class BlogsCategory extends Model
{
    public function blogs_data()
    {
        return $this->belongsToMany(Blogs::class);
    }
}

我想获取所有语言列为 'fa' 的博客帖子及其类别,并显示在我的视图中。 这是我的查看代码:

@forelse($data as $tmp)
                        <div class="blog-item-left">
                            <div class="blog-left-img">
                                <img src="/upload/blogs/small/{{$tmp->img}}" alt="{{$tmp->title}}" />
                            </div>
                            <div class="blog-left-text">

                                <div class="blog-text">
                                    <span><i class="fas fa-box"></i>
                                    @foreach($data->categories as $cat_data)
                                        {{$cat_data->title}}
                                    @endforeach
                                    </span>

                                    <a href="#"><h4 class="title">{{$tmp->title}}</h4></a>
                                    <p>{{$tmp->description}}</p>
                                </div>
                            </div>
                        </div>




                        @empty
                        <h2>No Data to show!</h2>
                    @endforelse

此代码向我显示此错误:

未定义属性:Illuminate\Pagination\LengthAwarePaginator::$类别(视图:/Users/soroush/Sites/msadd/resources/views/Dynamics/Blogs/Main.blade.php)

我必须做什么?

您只想加载 "categories" 关系与您的查询。

class BlogsController extends Controller
{
    public function main(){
        //$posts = new Blogs::where('lang',LaravelLocalization::getCurrent);
            $data = Blogs::with('categories')
                ->where('lang',LaravelLocalization::getCurrentLocale())
                ->where('created_at','<=', now())
                ->orderBy('created_at','DESC')
                ->paginate(config('PAGINATOR_COUNTER'));

                return view('Dynamics.Blogs.Main',compact('data'));
            }
}

然后像这样更改您的 blade(视图)模板 "categories" 迭代:

@foreach($tmp->categories as $cat_data)
    {{$cat_data->title}}
@endforeach