我有两个模型主题和流。 这里 主题属于多对流和 流有很多主题 是关系。
如何在主题中加载流的内容。
Subject.php
<?php
命名空间App \ Model \ Admin;
使用Illuminate \ Database \ Eloquent \ Model;
class主题扩展Model { 保护$ guarded = [];
public function streams()
{
return $this->belongsToMany(Stream::class);
}
public function chapters()
{
return $this->hasMany(Chapter::class);
}
} ?>
Stream.php
<?php
命名空间App \ Model \ Admin;
使用Illuminate \ Database \ Eloquent \ Model;
class Stream扩展了Model { 保护$ guarded = [];
public function faculty()
{
return $this->belongsTo(Faculty::class);
}
public function subjects()
{
return $this->hasMany(Subject::class);
}
}
?>
我的尝试:
@if (!empty($subject))
@foreach ($subject as $key => $subjects)
<tr>
<td>
{{ $key + 1 }}
</td>
<td>
{{$subjects->title}}
</td>
@foreach($subjects->streams as $item)
<td>
{{ $item->name}}
</td>
@endforeach
<td>
{{$subjects->description}}
</td>
@endforeach
@endif
但是它给出了错误:
SQLSTATE [42S02]:找不到基表或视图:1146表'pu_notes_db.stream_subject'不存在(SQL:选择streams
。*,stream_subject
。subject_id
为pivot_subject_id
,stream_subject
。stream_id
作为pivot_stream_id
的{{1}},来自streams
上的stream_subject
内部联接streams
。id
= { {1}}。stream_subject
,其中stream_id
。stream_subject
= 1)
我已经迁移了所有表格。 搜索了问题,但没有找到令人信服的解决方案。 请帮助。