简体   繁体   English

Laravel数据库关系不会加载相关数据(编辑)

[英]Laravel Database Relationship won't load associated data (edit)

I am trying to make a forum with sections and categories in laravel 5.7. 我试图在laravel 5.7中创建一个包含部分和类别的论坛。 All these sections has multiple categories. 所有这些部分都有多个类别。

When I build my relationship en try to get the data in the view, I get a error: 当我建立关系时,尝试在视图中获取数据时,出现错误:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 
'forum_categories.forum_section_id' in 'where clause' (SQL: select * from `forum_categories` where `forum_categories`.`forum_section_id` = 1 and `forum_categories`.`forum_section_id` is not null) 

This is my blade: 这是我的刀片:

@foreach($sections as $section) 
                        <h1 data-toggle="collapse" data-target="#section{{$section->id}}">{{$section->name}}</h1>
                        <div class="collapse" id="section{{$section->id}}">
                            Dit is sectie
                            @foreach($section->ForumCategory as $forumCategory)
                                {{$forumCategory->id}}
                                @endforeach
                        </div>
 @endforeach

My Controller function index(): 我的控制器功能index():

    $sections = ForumSection::all();
    return view('forum.index', compact(['sections'])); 

And this are My models: ForumCategory 这是我的模型: ForumCategory

namespace App;

use Illuminate\Database\Eloquent\Model;


class ForumCategory extends Model
{
  protected $fillable = [
     'name', 'active', 'position', 'section_id'
  ];
  public function ForumSection() {
     return $this->belongsTo(ForumSection::class, 'section_id');
  }
}

Model ForumSection : 示范论坛部分

 <?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class ForumSection extends Model
{
    protected $fillable = [
        'name','active', 'position'
    ];
    public function ForumCategory() {
        return $this->hasMany(ForumCategory::class, 'section_id');
    }
}

And Last: My 2 database tables: 最后:我的2个数据库表:

ForumSectionsTable 论坛部分表

public function up()
    {
            Schema::create('forum_sections', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->boolean('active');
            $table->integer('position')->unique();
            $table->timestamps();
    });
}

ForumCategoriesTable 论坛类别表

    public function up()
{
    Schema::create('forum_categories', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->integer('active');
        $table->integer('section_id')->unsigned();
        $table->timestamps();
    });
    Schema::table('forum_categories', function (Blueprint $table)
    {
        $table->foreign('section_id')->references('id')->on('forum_sections');
    });
}

Can someone tell me what I am did wrong? 有人可以告诉我我做错了什么吗?

This one should be pretty clear... It's looking for 这应该很清楚...它正在寻找

'forum_categories.forum_section_id'

as your linking id, but you're using 作为您的链接ID,但您正在使用

Schema::create('forum_categories', function (Blueprint $table) {
  ...
  $table->integer('section_id')->unsigned();
});

To solve this, you have a couple options: Override your migration and use forum_section_id instead of section_id , or add the ID to your relationship: 要解决此问题,您有两种选择:覆盖迁移并使用forum_section_id而不是section_id ,或将ID添加到您的关系中:

return $this->hasMany(ForumCategory::class, 'section_id');

You did that correctly for the belongsTo method, but seem to have missed it on the hasMany (inverse) method. 您已经为belongsTo方法正确地做到了这belongsTo ,但是似乎在hasMany (反向)方法上错过了它。

Next, use a loop to get a single id , as $seciton->ForumCategory is a Collection , and not a single Model: 接下来,使用循环获取单个id ,因为$seciton->ForumCategoryCollection ,而不是单个Model:

@foreach($section->ForumCateogry AS $forumCateogry)
Dit is sectie {{ $forumCateogry->id }}
@endforeach

That brings up the note on naming conventions; 这引起了有关命名约定的注意; method names are usually "camelCase", so forumCateogry() instead of ForumCateogry() , and pluralized for methods that return multiple, so forumCateogries() instead of forumCateogry() 方法名称通常为“ camelCase”,因此, forumCateogry()而不是ForumCateogry() ,并且对于返回多个值的方法,它们是复数的,因此, forumCateogries()而不是forumCateogry()

解决的方法很简单...我将ForumCategory和ForumSection重命名为Category和Section,问题就消失了。

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

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