在尝试了我在Google上找到的所有解决方案后,我似乎无法访问我的关系数据。我一直收到以下错误:
Trying to get property of non-object (View: /home/eneko/foundry/biome/resources/views/home.blade.php)
以下是为此路线加载的文件: Fruitu.php
<?php namespace Biome;
use Illuminate\Database\Eloquent\Model;
use Cviebrock\EloquentSluggable\SluggableInterface;
use Cviebrock\EloquentSluggable\SluggableTrait;
class Fruitu extends Model implements SluggableInterface {
use SluggableTrait;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'fruitus';
public function toString() {
return $this->izenburua;
}
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['izenburua', 'irudia', 'edukia'];
public function egilea() {
return $this->belongsTo('Biome\User');
}
protected $sluggable = array(
'build_from' => 'izenburua',
'save_to' => 'slug',
);
}
控制器功能:
public function index()
{
$fruituak = Fruitu::all();
$fruituak->load('egilea');
return view('home')->with(compact('fruituak'));
}
刀片模板中的循环:
@foreach ($fruituak as $fruitu)
<div class="fruitua">
<a href="{{ route('fruitu.show', $fruitu->slug) }}">
<h3>{{ $fruitu->izenburua }}</h3>
</a>
{{ $fruitu->egilea->name }}
<p class="eduk">{{ $fruitu->edukia }}</p>
</div>
@endforeach
提前感谢您的帮助!