【问题标题】:Laravel 5.7 Many To Many Relation Problem With PaginationLaravel 5.7 多对多关系问题与分页
【发布时间】:2018-09-22 09:43:45
【问题描述】:

我对 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);
    }
}

和 BlogsCategory 模型:

<?php

namespace App;

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

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

我想获取所有博客帖子,他们的 lang col 是 '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::$categories(查看:/Users/soroush/Sites/msadd/resources/views/Dynamics/Blogs/Main.blade.php)

我必须做什么?

【问题讨论】:

    标签: laravel model relation


    【解决方案1】:

    您只想在查询中加载“类别”关系。

    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'));
                }
    }
    

    并像这样更改刀片(视图)模板“类别”迭代:

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

    【讨论】:

    • 感谢您的回答,但现在这段代码向我显示了这个错误,我认为我的视图代码也有一些问题:未定义的属性:Illuminate\Pagination\LengthAwarePaginator::$categories (View: /Users/ soroush/Sites/msadd/resources/views/Dynamics/Blogs/Main.blade.php)
    • 您查询的第一个问题。您可以使用此更改您的查询。我目前看到的视图模板的另一个问题。所以我正在更改我的回复。
    • mark the answer as accepted。这会向其他用户显示问题已解决。
    猜你喜欢
    • 2019-06-03
    • 2014-11-11
    • 2015-09-14
    • 1970-01-01
    • 2015-04-23
    • 2019-05-06
    • 2014-05-24
    • 2019-11-25
    • 1970-01-01
    相关资源
    最近更新 更多