Help with error Trying to get Property of non-object Laravel

Asked

Viewed 1,800 times

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?

1 answer

0


Always use early loading, because, it generates only 2 SQL and with that there is no performance loss, example of the modification:

public function index()
{
    $cursos = Curso::with('professor')->get();

    return view('curso.index', compact('cursos'));
}

other points have typo, example is not only App\Curso, or is "App\Curso" or App\Curso::class, then modified the two classes:

<?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::class);
    }
}

and

<?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::class);
    }
}
  • My table is called teachers with S in the end, I put the same?

  • @Lintonjunior actually there is the name of the method that in the case I have now changed to professor !!! ? check out

  • (1/1) Relationnotfoundexception Call to Undefined Relationship [professor] on model [flexpeak Curso]. Returned this

  • Had typo @Lintonjunior take a look!

  • @Lintonjunior one more change.

  • 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);?

  • 1

    I do, buddy. Thanks a lot for your help

  • 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?

  • @Lintonjunior each question we answer a question, then, can open a new question to a new doubt without problems! OK!

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.