Reputation: 25
I am trying to make a forum with sections and categories in 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():
$sections = ForumSection::all();
return view('forum.index', compact(['sections']));
And this are My models: 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:
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?
Upvotes: 0
Views: 99
Reputation: 25
The solution was simple... I renamed ForumCategory and ForumSection to Category and Section and the problem was gone... I've learned a lesson ;)
Upvotes: 0
Reputation: 29288
This one should be pretty clear... It's looking for
'forum_categories.forum_section_id'
as your linking id, but you're using
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:
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.
Next, use a loop to get a single id
, as $seciton->ForumCategory
is a Collection
, and not a single 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()
Upvotes: 2