I have a relationship, where I try to get the name of a teacher, I can return the id of it, but when I try to return the name it gives me the message Trying to get property of non-object My code looks like this: Model Course.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Curso extends Model
{
protected $fillable = [
'professor_id',
'nome'
];
public function professor()
{
return $this->belongsTo(App\Professor);
}
}
Model Teacher
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Professor extends Model
{
protected $fillable = [
'nome',
'data_nascimento'
];
public function cursos()
{
return $this->hasMany(App\Curso);
}
}
CourseController
*/
public function index()
{
$cursos = Curso::all();
return view('curso.index', compact('cursos'));
}
And my index.blade.php
@foreach($cursos as $value)
<tr>
<td>{{$value->id}}</td>
<td>{{$value->nome}}</td>
<td>{{ $value->professor->nome }}</td>
<td>{{$value->created_at}}</td>
Here it gives the error, exactly in {{$ Value-> professors-> name}} My table is called professors by the way.
Can someone give me a light?