我有这个雄辩的问题,我试图结合这个3表来显示视图页面上一个表中的数据。但它转向了错误。请帮我。
我是否在编码或表关系上做了任何错误?
型号:
TreeCategory模型
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\TreeScientific;
class TreeCategory extends Model
{
protected $fillable = [
'id',
'code',
'category',
];
public function scientific()
{
return $this->hasMany('App\TreeScientific','tree_category_id');
}
}
TreeScientific
命名空间App;
use Illuminate\Database\Eloquent\Model;
use App\TreeCommon;
class TreeScientific extends Model
{
protected $fillable = [
'tree_category_id',
'species_code',
'scientific_name',
];
public function common()
{
return $this->hasOne('App\TreeCommon','scientific_id');
}
}
TreeCommons
namespace App;
use Illuminate\Database\Eloquent\Model;
class TreeCommon extends Model
{
protected $fillable = [
'scientific_id',
'common_name',
];
}
Blade.php
<tbody>
@foreach ($category as $kategori)
<tr>
<td>{{$loop -> iteration}}</td>
<td>{{$kategori -> code}}</td>
<td>{{$kategori -> scientific -> species_code}}</td>
<td>{{$kategori -> scientific -> scientific_name}}</td>
<td>{{$kategori -> scientific -> common -> common_name}}</td>
@endforeach
控制器
使用App \ TreeCategory; 使用App \ TreeScientific; 使用App \ TreeCommon;
class PokokController extends Controller { public function pokokIndex() { $category = TreeCategory::all(); $scientific = TreeScientific::all(); $common = TreeCommon::all(); return view('')->with('category',$category)- >with('scientific',$scientific)->with('common',$common); } }