1
I have a relationship, where I try to get the name of a teacher, I can return his id, but when I try to return the name it gives me the message Trying to get Property of non-object My code is like this: Model Curso.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 Professor
<?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);
}
}
Cursocontroller
*/
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 teachers by the way.
Someone can give me a light?
My table is called teachers with S in the end, I put the same?
– Linton Junior
@Lintonjunior actually there is the name of the method that in the case I have now changed to
professor
!!! ? check out– novic
(1/1) Relationnotfoundexception Call to Undefined Relationship [professor] on model [flexpeak Curso]. Returned this
– Linton Junior
Had typo @Lintonjunior take a look!
– novic
@Lintonjunior one more change.
– novic
Now it worked, Pop, man. What a big help. The error was only in Return $this->hasMany( App Curso::class); and Return $this->belongsTo( App Professor::class);?
– Linton Junior
I do, buddy. Thanks a lot for your help
– Linton Junior
Virgilio, can you give me another hand? No create e Edit, how do I call and in the corresponding views how do I call a select?
– Linton Junior
@Lintonjunior each question we answer a question, then, can open a new question to a new doubt without problems! OK!
– novic